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/drive_api_requests.h" | 5 #include "google_apis/drive/drive_api_requests.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "google_apis/drive/request_sender.h" | 14 #include "google_apis/drive/request_sender.h" |
| 15 #include "google_apis/drive/request_util.h" | 15 #include "google_apis/drive/request_util.h" |
| 16 #include "google_apis/drive/time_util.h" | 16 #include "google_apis/drive/time_util.h" |
| 17 #include "net/base/url_util.h" | 17 #include "net/base/url_util.h" |
| 18 | 18 |
| 19 namespace google_apis { | 19 namespace google_apis { |
| 20 namespace drive { | |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // Parses the JSON value to FileResource instance and runs |callback| on the | 23 // Parses the JSON value to FileResource instance and runs |callback| on the |
| 23 // UI thread once parsing is done. | 24 // UI thread once parsing is done. |
| 24 // This is customized version of ParseJsonAndRun defined above to adapt the | 25 // This is customized version of ParseJsonAndRun defined above to adapt the |
| 25 // remaining response type. | 26 // remaining response type. |
| 26 void ParseFileResourceWithUploadRangeAndRun( | 27 void ParseFileResourceWithUploadRangeAndRun(const UploadRangeCallback& callback, |
| 27 const drive::UploadRangeCallback& callback, | 28 const UploadRangeResponse& response, |
| 28 const UploadRangeResponse& response, | 29 scoped_ptr<base::Value> value) { |
| 29 scoped_ptr<base::Value> value) { | |
| 30 DCHECK(!callback.is_null()); | 30 DCHECK(!callback.is_null()); |
| 31 | 31 |
| 32 scoped_ptr<FileResource> file_resource; | 32 scoped_ptr<FileResource> file_resource; |
| 33 if (value) { | 33 if (value) { |
| 34 file_resource = FileResource::CreateFrom(*value); | 34 file_resource = FileResource::CreateFrom(*value); |
| 35 if (!file_resource) { | 35 if (!file_resource) { |
| 36 callback.Run( | 36 callback.Run( |
| 37 UploadRangeResponse(DRIVE_PARSE_ERROR, | 37 UploadRangeResponse(DRIVE_PARSE_ERROR, |
| 38 response.start_position_received, | 38 response.start_position_received, |
| 39 response.end_position_received), | 39 response.end_position_received), |
| 40 scoped_ptr<FileResource>()); | 40 scoped_ptr<FileResource>()); |
| 41 return; | 41 return; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 callback.Run(response, file_resource.Pass()); | 45 callback.Run(response, file_resource.Pass()); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Attaches |properties| to the |request_body| if |properties| is not empty. | |
| 49 // |request_body| must not be NULL. | |
| 50 void AttachProperties(const Properties& properties, | |
| 51 base::DictionaryValue* request_body) { | |
| 52 DCHECK(request_body); | |
| 53 if (properties.empty()) | |
| 54 return; | |
| 55 | |
| 56 base::ListValue* const properties_value = new base::ListValue; | |
| 57 for (const auto& property : properties) { | |
| 58 base::DictionaryValue* const property_value = new base::DictionaryValue; | |
| 59 std::string visibility_as_string; | |
| 60 switch (property.visibility()) { | |
| 61 case Property::VISIBILITY_PRIVATE: | |
| 62 visibility_as_string = "PRIVATE"; | |
| 63 break; | |
| 64 case Property::VISIBILITY_PUBLIC: | |
| 65 visibility_as_string = "PUBLIC"; | |
| 66 break; | |
| 67 } | |
| 68 property_value->SetString("visibility", visibility_as_string); | |
| 69 property_value->SetString("key", property.key()); | |
| 70 property_value->SetString("value", property.value()); | |
| 71 properties_value->Append(property_value); | |
| 72 } | |
| 73 request_body->Set("properties", properties_value); | |
| 74 } | |
| 75 | |
| 76 // Creates metadata JSON string for multipart uploading. | |
| 77 // All the values are optional. If the value is empty or null, the value does | |
| 78 // not appear in the metadata. | |
| 79 std::string CreateMultipartUploadMetadataJson( | |
| 80 const std::string& title, | |
| 81 const std::string& parent_resource_id, | |
| 82 const base::Time& modified_date, | |
| 83 const base::Time& last_viewed_by_me_date, | |
| 84 const Properties& properties) { | |
| 85 base::DictionaryValue root; | |
| 86 if (!title.empty()) | |
| 87 root.SetString("title", title); | |
| 88 | |
| 89 // Fill parent link. | |
| 90 if (!parent_resource_id.empty()) { | |
| 91 scoped_ptr<base::ListValue> parents(new base::ListValue); | |
| 92 parents->Append( | |
| 93 google_apis::util::CreateParentValue(parent_resource_id).release()); | |
| 94 root.Set("parents", parents.release()); | |
| 95 } | |
| 96 | |
| 97 if (!modified_date.is_null()) | |
|
hashimoto
2015/02/23 10:16:20
nit: Please add {} while you are here.
mtomasz
2015/02/24 02:30:37
Done.
| |
| 98 root.SetString("modifiedDate", | |
| 99 google_apis::util::FormatTimeAsString(modified_date)); | |
| 100 | |
| 101 if (!last_viewed_by_me_date.is_null()) { | |
| 102 root.SetString("lastViewedByMeDate", google_apis::util::FormatTimeAsString( | |
| 103 last_viewed_by_me_date)); | |
| 104 } | |
| 105 | |
| 106 AttachProperties(properties, &root); | |
| 107 std::string json_string; | |
| 108 base::JSONWriter::Write(&root, &json_string); | |
| 109 return json_string; | |
| 110 } | |
| 111 | |
| 48 } // namespace | 112 } // namespace |
| 49 | 113 |
| 50 namespace drive { | |
| 51 | |
| 52 Property::Property() : visibility_(VISIBILITY_PRIVATE) { | 114 Property::Property() : visibility_(VISIBILITY_PRIVATE) { |
| 53 } | 115 } |
| 54 | 116 |
| 55 Property::~Property() { | 117 Property::~Property() { |
| 56 } | 118 } |
| 57 | 119 |
| 58 //============================ DriveApiPartialFieldRequest ==================== | 120 //============================ DriveApiPartialFieldRequest ==================== |
| 59 | 121 |
| 60 DriveApiPartialFieldRequest::DriveApiPartialFieldRequest( | 122 DriveApiPartialFieldRequest::DriveApiPartialFieldRequest( |
| 61 RequestSender* sender) : UrlFetchRequestBase(sender) { | 123 RequestSender* sender) : UrlFetchRequestBase(sender) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 base::DictionaryValue* parent = new base::DictionaryValue; | 215 base::DictionaryValue* parent = new base::DictionaryValue; |
| 154 parent->SetString("id", parents_[i]); | 216 parent->SetString("id", parents_[i]); |
| 155 parents_value->Append(parent); | 217 parents_value->Append(parent); |
| 156 } | 218 } |
| 157 root.Set("parents", parents_value); | 219 root.Set("parents", parents_value); |
| 158 } | 220 } |
| 159 | 221 |
| 160 if (!title_.empty()) | 222 if (!title_.empty()) |
| 161 root.SetString("title", title_); | 223 root.SetString("title", title_); |
| 162 | 224 |
| 225 AttachProperties(properties_, &root); | |
| 163 base::JSONWriter::Write(&root, upload_content); | 226 base::JSONWriter::Write(&root, upload_content); |
| 227 | |
| 164 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", [" | 228 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", [" |
| 165 << *upload_content << "]"; | 229 << *upload_content << "]"; |
| 166 return true; | 230 return true; |
| 167 } | 231 } |
| 168 | 232 |
| 169 GURL FilesInsertRequest::GetURLInternal() const { | 233 GURL FilesInsertRequest::GetURLInternal() const { |
| 170 return url_generator_.GetFilesInsertUrl(); | 234 return url_generator_.GetFilesInsertUrl(); |
| 171 } | 235 } |
| 172 | 236 |
| 173 //============================== FilesPatchRequest ============================ | 237 //============================== FilesPatchRequest ============================ |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 if (!parents_.empty()) { | 289 if (!parents_.empty()) { |
| 226 base::ListValue* parents_value = new base::ListValue; | 290 base::ListValue* parents_value = new base::ListValue; |
| 227 for (size_t i = 0; i < parents_.size(); ++i) { | 291 for (size_t i = 0; i < parents_.size(); ++i) { |
| 228 base::DictionaryValue* parent = new base::DictionaryValue; | 292 base::DictionaryValue* parent = new base::DictionaryValue; |
| 229 parent->SetString("id", parents_[i]); | 293 parent->SetString("id", parents_[i]); |
| 230 parents_value->Append(parent); | 294 parents_value->Append(parent); |
| 231 } | 295 } |
| 232 root.Set("parents", parents_value); | 296 root.Set("parents", parents_value); |
| 233 } | 297 } |
| 234 | 298 |
| 235 if (!properties_.empty()) { | 299 AttachProperties(properties_, &root); |
| 236 base::ListValue* properties_value = new base::ListValue; | 300 base::JSONWriter::Write(&root, upload_content); |
| 237 for (const auto& property : properties_) { | |
| 238 base::DictionaryValue* const property_value = new base::DictionaryValue; | |
| 239 std::string visibility_as_string; | |
| 240 switch (property.visibility()) { | |
| 241 case Property::VISIBILITY_PRIVATE: | |
| 242 visibility_as_string = "PRIVATE"; | |
| 243 break; | |
| 244 case Property::VISIBILITY_PUBLIC: | |
| 245 visibility_as_string = "PUBLIC"; | |
| 246 break; | |
| 247 } | |
| 248 property_value->SetString("visibility", visibility_as_string); | |
| 249 property_value->SetString("key", property.key()); | |
| 250 property_value->SetString("value", property.value()); | |
| 251 properties_value->Append(property_value); | |
| 252 } | |
| 253 root.Set("properties", properties_value); | |
| 254 } | |
| 255 | 301 |
| 256 base::JSONWriter::Write(&root, upload_content); | |
| 257 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" | 302 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" |
| 258 << *upload_content << "]"; | 303 << *upload_content << "]"; |
| 259 return true; | 304 return true; |
| 260 } | 305 } |
| 261 | 306 |
| 262 //============================= FilesCopyRequest ============================== | 307 //============================= FilesCopyRequest ============================== |
| 263 | 308 |
| 264 FilesCopyRequest::FilesCopyRequest( | 309 FilesCopyRequest::FilesCopyRequest( |
| 265 RequestSender* sender, | 310 RequestSender* sender, |
| 266 const DriveApiUrlGenerator& url_generator, | 311 const DriveApiUrlGenerator& url_generator, |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 root.Set("parents", parents.release()); | 632 root.Set("parents", parents.release()); |
| 588 | 633 |
| 589 if (!modified_date_.is_null()) | 634 if (!modified_date_.is_null()) |
| 590 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 635 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 591 | 636 |
| 592 if (!last_viewed_by_me_date_.is_null()) { | 637 if (!last_viewed_by_me_date_.is_null()) { |
| 593 root.SetString("lastViewedByMeDate", | 638 root.SetString("lastViewedByMeDate", |
| 594 util::FormatTimeAsString(last_viewed_by_me_date_)); | 639 util::FormatTimeAsString(last_viewed_by_me_date_)); |
| 595 } | 640 } |
| 596 | 641 |
| 642 AttachProperties(properties_, &root); | |
| 597 base::JSONWriter::Write(&root, upload_content); | 643 base::JSONWriter::Write(&root, upload_content); |
| 598 | 644 |
| 599 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", [" | 645 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", [" |
| 600 << *upload_content << "]"; | 646 << *upload_content << "]"; |
| 601 return true; | 647 return true; |
| 602 } | 648 } |
| 603 | 649 |
| 604 //===================== InitiateUploadExistingFileRequest ==================== | 650 //===================== InitiateUploadExistingFileRequest ==================== |
| 605 | 651 |
| 606 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest( | 652 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 654 root.SetString("title", title_); | 700 root.SetString("title", title_); |
| 655 | 701 |
| 656 if (!modified_date_.is_null()) | 702 if (!modified_date_.is_null()) |
| 657 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 703 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 658 | 704 |
| 659 if (!last_viewed_by_me_date_.is_null()) { | 705 if (!last_viewed_by_me_date_.is_null()) { |
| 660 root.SetString("lastViewedByMeDate", | 706 root.SetString("lastViewedByMeDate", |
| 661 util::FormatTimeAsString(last_viewed_by_me_date_)); | 707 util::FormatTimeAsString(last_viewed_by_me_date_)); |
| 662 } | 708 } |
| 663 | 709 |
| 710 AttachProperties(properties_, &root); | |
| 664 if (root.empty()) | 711 if (root.empty()) |
| 665 return false; | 712 return false; |
| 666 | 713 |
| 667 *upload_content_type = util::kContentTypeApplicationJson; | 714 *upload_content_type = util::kContentTypeApplicationJson; |
| 668 base::JSONWriter::Write(&root, upload_content); | 715 base::JSONWriter::Write(&root, upload_content); |
| 669 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type | 716 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type |
| 670 << ", [" << *upload_content << "]"; | 717 << ", [" << *upload_content << "]"; |
| 671 return true; | 718 return true; |
| 672 } | 719 } |
| 673 | 720 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 | 784 |
| 738 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest( | 785 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest( |
| 739 RequestSender* sender, | 786 RequestSender* sender, |
| 740 const std::string& title, | 787 const std::string& title, |
| 741 const std::string& parent_resource_id, | 788 const std::string& parent_resource_id, |
| 742 const std::string& content_type, | 789 const std::string& content_type, |
| 743 int64 content_length, | 790 int64 content_length, |
| 744 const base::Time& modified_date, | 791 const base::Time& modified_date, |
| 745 const base::Time& last_viewed_by_me_date, | 792 const base::Time& last_viewed_by_me_date, |
| 746 const base::FilePath& local_file_path, | 793 const base::FilePath& local_file_path, |
| 794 const Properties& properties, | |
| 747 const DriveApiUrlGenerator& url_generator, | 795 const DriveApiUrlGenerator& url_generator, |
| 748 const FileResourceCallback& callback, | 796 const FileResourceCallback& callback, |
| 749 const ProgressCallback& progress_callback) | 797 const ProgressCallback& progress_callback) |
| 750 : MultipartUploadRequestBase(sender, | 798 : MultipartUploadRequestBase( |
| 751 title, | 799 sender, |
| 752 parent_resource_id, | 800 CreateMultipartUploadMetadataJson(title, |
| 753 content_type, | 801 parent_resource_id, |
| 754 content_length, | 802 modified_date, |
| 755 modified_date, | 803 last_viewed_by_me_date, |
| 756 last_viewed_by_me_date, | 804 properties), |
| 757 local_file_path, | 805 content_type, |
| 758 callback, | 806 content_length, |
| 759 progress_callback), | 807 local_file_path, |
| 808 callback, | |
| 809 progress_callback), | |
| 810 has_modified_date_(!modified_date.is_null()), | |
| 760 url_generator_(url_generator) { | 811 url_generator_(url_generator) { |
| 761 } | 812 } |
| 762 | 813 |
| 763 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() { | 814 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() { |
| 764 } | 815 } |
| 765 | 816 |
| 766 GURL MultipartUploadNewFileRequest::GetURL() const { | 817 GURL MultipartUploadNewFileRequest::GetURL() const { |
| 767 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date()); | 818 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date_); |
| 768 } | 819 } |
| 769 | 820 |
| 770 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType() | 821 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType() |
| 771 const { | 822 const { |
| 772 return net::URLFetcher::POST; | 823 return net::URLFetcher::POST; |
| 773 } | 824 } |
| 774 | 825 |
| 775 //======================= MultipartUploadExistingFileRequest =================== | 826 //======================= MultipartUploadExistingFileRequest =================== |
| 776 | 827 |
| 777 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest( | 828 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest( |
| 778 RequestSender* sender, | 829 RequestSender* sender, |
| 779 const std::string& title, | 830 const std::string& title, |
| 780 const std::string& resource_id, | 831 const std::string& resource_id, |
| 781 const std::string& parent_resource_id, | 832 const std::string& parent_resource_id, |
| 782 const std::string& content_type, | 833 const std::string& content_type, |
| 783 int64 content_length, | 834 int64 content_length, |
| 784 const base::Time& modified_date, | 835 const base::Time& modified_date, |
| 785 const base::Time& last_viewed_by_me_date, | 836 const base::Time& last_viewed_by_me_date, |
| 786 const base::FilePath& local_file_path, | 837 const base::FilePath& local_file_path, |
| 787 const std::string& etag, | 838 const std::string& etag, |
| 839 const Properties& properties, | |
| 788 const DriveApiUrlGenerator& url_generator, | 840 const DriveApiUrlGenerator& url_generator, |
| 789 const FileResourceCallback& callback, | 841 const FileResourceCallback& callback, |
| 790 const ProgressCallback& progress_callback) | 842 const ProgressCallback& progress_callback) |
| 791 : MultipartUploadRequestBase(sender, | 843 : MultipartUploadRequestBase( |
| 792 title, | 844 sender, |
| 793 parent_resource_id, | 845 CreateMultipartUploadMetadataJson(title, |
| 794 content_type, | 846 parent_resource_id, |
| 795 content_length, | 847 modified_date, |
| 796 modified_date, | 848 last_viewed_by_me_date, |
| 797 last_viewed_by_me_date, | 849 properties), |
| 798 local_file_path, | 850 content_type, |
| 799 callback, | 851 content_length, |
| 800 progress_callback), | 852 local_file_path, |
| 853 callback, | |
| 854 progress_callback), | |
| 801 resource_id_(resource_id), | 855 resource_id_(resource_id), |
| 802 etag_(etag), | 856 etag_(etag), |
| 857 has_modified_date_(!modified_date.is_null()), | |
| 803 url_generator_(url_generator) { | 858 url_generator_(url_generator) { |
| 804 } | 859 } |
| 805 | 860 |
| 806 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() { | 861 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() { |
| 807 } | 862 } |
| 808 | 863 |
| 809 std::vector<std::string> | 864 std::vector<std::string> |
| 810 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const { | 865 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const { |
| 811 std::vector<std::string> headers( | 866 std::vector<std::string> headers( |
| 812 MultipartUploadRequestBase::GetExtraRequestHeaders()); | 867 MultipartUploadRequestBase::GetExtraRequestHeaders()); |
| 813 headers.push_back(util::GenerateIfMatchHeader(etag_)); | 868 headers.push_back(util::GenerateIfMatchHeader(etag_)); |
| 814 return headers; | 869 return headers; |
| 815 } | 870 } |
| 816 | 871 |
| 817 GURL MultipartUploadExistingFileRequest::GetURL() const { | 872 GURL MultipartUploadExistingFileRequest::GetURL() const { |
| 818 return url_generator_.GetMultipartUploadExistingFileUrl( | 873 return url_generator_.GetMultipartUploadExistingFileUrl(resource_id_, |
| 819 resource_id_, has_modified_date()); | 874 has_modified_date_); |
| 820 } | 875 } |
| 821 | 876 |
| 822 net::URLFetcher::RequestType | 877 net::URLFetcher::RequestType |
| 823 MultipartUploadExistingFileRequest::GetRequestType() const { | 878 MultipartUploadExistingFileRequest::GetRequestType() const { |
| 824 return net::URLFetcher::PUT; | 879 return net::URLFetcher::PUT; |
| 825 } | 880 } |
| 826 | 881 |
| 827 //========================== DownloadFileRequest ========================== | 882 //========================== DownloadFileRequest ========================== |
| 828 | 883 |
| 829 DownloadFileRequest::DownloadFileRequest( | 884 DownloadFileRequest::DownloadFileRequest( |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 908 } | 963 } |
| 909 break; | 964 break; |
| 910 } | 965 } |
| 911 root.SetString("value", value_); | 966 root.SetString("value", value_); |
| 912 base::JSONWriter::Write(&root, upload_content); | 967 base::JSONWriter::Write(&root, upload_content); |
| 913 return true; | 968 return true; |
| 914 } | 969 } |
| 915 | 970 |
| 916 } // namespace drive | 971 } // namespace drive |
| 917 } // namespace google_apis | 972 } // namespace google_apis |
| OLD | NEW |