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()) { |
| 98 root.SetString("modifiedDate", |
| 99 google_apis::util::FormatTimeAsString(modified_date)); |
| 100 } |
| 101 |
| 102 if (!last_viewed_by_me_date.is_null()) { |
| 103 root.SetString("lastViewedByMeDate", google_apis::util::FormatTimeAsString( |
| 104 last_viewed_by_me_date)); |
| 105 } |
| 106 |
| 107 AttachProperties(properties, &root); |
| 108 std::string json_string; |
| 109 base::JSONWriter::Write(&root, &json_string); |
| 110 return json_string; |
| 111 } |
| 112 |
48 } // namespace | 113 } // namespace |
49 | 114 |
50 namespace drive { | |
51 | |
52 Property::Property() : visibility_(VISIBILITY_PRIVATE) { | 115 Property::Property() : visibility_(VISIBILITY_PRIVATE) { |
53 } | 116 } |
54 | 117 |
55 Property::~Property() { | 118 Property::~Property() { |
56 } | 119 } |
57 | 120 |
58 //============================ DriveApiPartialFieldRequest ==================== | 121 //============================ DriveApiPartialFieldRequest ==================== |
59 | 122 |
60 DriveApiPartialFieldRequest::DriveApiPartialFieldRequest( | 123 DriveApiPartialFieldRequest::DriveApiPartialFieldRequest( |
61 RequestSender* sender) : UrlFetchRequestBase(sender) { | 124 RequestSender* sender) : UrlFetchRequestBase(sender) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 base::DictionaryValue* parent = new base::DictionaryValue; | 216 base::DictionaryValue* parent = new base::DictionaryValue; |
154 parent->SetString("id", parents_[i]); | 217 parent->SetString("id", parents_[i]); |
155 parents_value->Append(parent); | 218 parents_value->Append(parent); |
156 } | 219 } |
157 root.Set("parents", parents_value); | 220 root.Set("parents", parents_value); |
158 } | 221 } |
159 | 222 |
160 if (!title_.empty()) | 223 if (!title_.empty()) |
161 root.SetString("title", title_); | 224 root.SetString("title", title_); |
162 | 225 |
| 226 AttachProperties(properties_, &root); |
163 base::JSONWriter::Write(&root, upload_content); | 227 base::JSONWriter::Write(&root, upload_content); |
| 228 |
164 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", [" | 229 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", [" |
165 << *upload_content << "]"; | 230 << *upload_content << "]"; |
166 return true; | 231 return true; |
167 } | 232 } |
168 | 233 |
169 GURL FilesInsertRequest::GetURLInternal() const { | 234 GURL FilesInsertRequest::GetURLInternal() const { |
170 return url_generator_.GetFilesInsertUrl(); | 235 return url_generator_.GetFilesInsertUrl(); |
171 } | 236 } |
172 | 237 |
173 //============================== FilesPatchRequest ============================ | 238 //============================== FilesPatchRequest ============================ |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 if (!parents_.empty()) { | 290 if (!parents_.empty()) { |
226 base::ListValue* parents_value = new base::ListValue; | 291 base::ListValue* parents_value = new base::ListValue; |
227 for (size_t i = 0; i < parents_.size(); ++i) { | 292 for (size_t i = 0; i < parents_.size(); ++i) { |
228 base::DictionaryValue* parent = new base::DictionaryValue; | 293 base::DictionaryValue* parent = new base::DictionaryValue; |
229 parent->SetString("id", parents_[i]); | 294 parent->SetString("id", parents_[i]); |
230 parents_value->Append(parent); | 295 parents_value->Append(parent); |
231 } | 296 } |
232 root.Set("parents", parents_value); | 297 root.Set("parents", parents_value); |
233 } | 298 } |
234 | 299 |
235 if (!properties_.empty()) { | 300 AttachProperties(properties_, &root); |
236 base::ListValue* properties_value = new base::ListValue; | 301 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 | 302 |
256 base::JSONWriter::Write(&root, upload_content); | |
257 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" | 303 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" |
258 << *upload_content << "]"; | 304 << *upload_content << "]"; |
259 return true; | 305 return true; |
260 } | 306 } |
261 | 307 |
262 //============================= FilesCopyRequest ============================== | 308 //============================= FilesCopyRequest ============================== |
263 | 309 |
264 FilesCopyRequest::FilesCopyRequest( | 310 FilesCopyRequest::FilesCopyRequest( |
265 RequestSender* sender, | 311 RequestSender* sender, |
266 const DriveApiUrlGenerator& url_generator, | 312 const DriveApiUrlGenerator& url_generator, |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 root.Set("parents", parents.release()); | 633 root.Set("parents", parents.release()); |
588 | 634 |
589 if (!modified_date_.is_null()) | 635 if (!modified_date_.is_null()) |
590 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 636 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
591 | 637 |
592 if (!last_viewed_by_me_date_.is_null()) { | 638 if (!last_viewed_by_me_date_.is_null()) { |
593 root.SetString("lastViewedByMeDate", | 639 root.SetString("lastViewedByMeDate", |
594 util::FormatTimeAsString(last_viewed_by_me_date_)); | 640 util::FormatTimeAsString(last_viewed_by_me_date_)); |
595 } | 641 } |
596 | 642 |
| 643 AttachProperties(properties_, &root); |
597 base::JSONWriter::Write(&root, upload_content); | 644 base::JSONWriter::Write(&root, upload_content); |
598 | 645 |
599 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", [" | 646 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", [" |
600 << *upload_content << "]"; | 647 << *upload_content << "]"; |
601 return true; | 648 return true; |
602 } | 649 } |
603 | 650 |
604 //===================== InitiateUploadExistingFileRequest ==================== | 651 //===================== InitiateUploadExistingFileRequest ==================== |
605 | 652 |
606 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest( | 653 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 root.SetString("title", title_); | 701 root.SetString("title", title_); |
655 | 702 |
656 if (!modified_date_.is_null()) | 703 if (!modified_date_.is_null()) |
657 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 704 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
658 | 705 |
659 if (!last_viewed_by_me_date_.is_null()) { | 706 if (!last_viewed_by_me_date_.is_null()) { |
660 root.SetString("lastViewedByMeDate", | 707 root.SetString("lastViewedByMeDate", |
661 util::FormatTimeAsString(last_viewed_by_me_date_)); | 708 util::FormatTimeAsString(last_viewed_by_me_date_)); |
662 } | 709 } |
663 | 710 |
| 711 AttachProperties(properties_, &root); |
664 if (root.empty()) | 712 if (root.empty()) |
665 return false; | 713 return false; |
666 | 714 |
667 *upload_content_type = util::kContentTypeApplicationJson; | 715 *upload_content_type = util::kContentTypeApplicationJson; |
668 base::JSONWriter::Write(&root, upload_content); | 716 base::JSONWriter::Write(&root, upload_content); |
669 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type | 717 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type |
670 << ", [" << *upload_content << "]"; | 718 << ", [" << *upload_content << "]"; |
671 return true; | 719 return true; |
672 } | 720 } |
673 | 721 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 | 785 |
738 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest( | 786 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest( |
739 RequestSender* sender, | 787 RequestSender* sender, |
740 const std::string& title, | 788 const std::string& title, |
741 const std::string& parent_resource_id, | 789 const std::string& parent_resource_id, |
742 const std::string& content_type, | 790 const std::string& content_type, |
743 int64 content_length, | 791 int64 content_length, |
744 const base::Time& modified_date, | 792 const base::Time& modified_date, |
745 const base::Time& last_viewed_by_me_date, | 793 const base::Time& last_viewed_by_me_date, |
746 const base::FilePath& local_file_path, | 794 const base::FilePath& local_file_path, |
| 795 const Properties& properties, |
747 const DriveApiUrlGenerator& url_generator, | 796 const DriveApiUrlGenerator& url_generator, |
748 const FileResourceCallback& callback, | 797 const FileResourceCallback& callback, |
749 const ProgressCallback& progress_callback) | 798 const ProgressCallback& progress_callback) |
750 : MultipartUploadRequestBase(sender, | 799 : MultipartUploadRequestBase( |
751 title, | 800 sender, |
752 parent_resource_id, | 801 CreateMultipartUploadMetadataJson(title, |
753 content_type, | 802 parent_resource_id, |
754 content_length, | 803 modified_date, |
755 modified_date, | 804 last_viewed_by_me_date, |
756 last_viewed_by_me_date, | 805 properties), |
757 local_file_path, | 806 content_type, |
758 callback, | 807 content_length, |
759 progress_callback), | 808 local_file_path, |
| 809 callback, |
| 810 progress_callback), |
| 811 has_modified_date_(!modified_date.is_null()), |
760 url_generator_(url_generator) { | 812 url_generator_(url_generator) { |
761 } | 813 } |
762 | 814 |
763 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() { | 815 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() { |
764 } | 816 } |
765 | 817 |
766 GURL MultipartUploadNewFileRequest::GetURL() const { | 818 GURL MultipartUploadNewFileRequest::GetURL() const { |
767 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date()); | 819 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date_); |
768 } | 820 } |
769 | 821 |
770 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType() | 822 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType() |
771 const { | 823 const { |
772 return net::URLFetcher::POST; | 824 return net::URLFetcher::POST; |
773 } | 825 } |
774 | 826 |
775 //======================= MultipartUploadExistingFileRequest =================== | 827 //======================= MultipartUploadExistingFileRequest =================== |
776 | 828 |
777 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest( | 829 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest( |
778 RequestSender* sender, | 830 RequestSender* sender, |
779 const std::string& title, | 831 const std::string& title, |
780 const std::string& resource_id, | 832 const std::string& resource_id, |
781 const std::string& parent_resource_id, | 833 const std::string& parent_resource_id, |
782 const std::string& content_type, | 834 const std::string& content_type, |
783 int64 content_length, | 835 int64 content_length, |
784 const base::Time& modified_date, | 836 const base::Time& modified_date, |
785 const base::Time& last_viewed_by_me_date, | 837 const base::Time& last_viewed_by_me_date, |
786 const base::FilePath& local_file_path, | 838 const base::FilePath& local_file_path, |
787 const std::string& etag, | 839 const std::string& etag, |
| 840 const Properties& properties, |
788 const DriveApiUrlGenerator& url_generator, | 841 const DriveApiUrlGenerator& url_generator, |
789 const FileResourceCallback& callback, | 842 const FileResourceCallback& callback, |
790 const ProgressCallback& progress_callback) | 843 const ProgressCallback& progress_callback) |
791 : MultipartUploadRequestBase(sender, | 844 : MultipartUploadRequestBase( |
792 title, | 845 sender, |
793 parent_resource_id, | 846 CreateMultipartUploadMetadataJson(title, |
794 content_type, | 847 parent_resource_id, |
795 content_length, | 848 modified_date, |
796 modified_date, | 849 last_viewed_by_me_date, |
797 last_viewed_by_me_date, | 850 properties), |
798 local_file_path, | 851 content_type, |
799 callback, | 852 content_length, |
800 progress_callback), | 853 local_file_path, |
| 854 callback, |
| 855 progress_callback), |
801 resource_id_(resource_id), | 856 resource_id_(resource_id), |
802 etag_(etag), | 857 etag_(etag), |
| 858 has_modified_date_(!modified_date.is_null()), |
803 url_generator_(url_generator) { | 859 url_generator_(url_generator) { |
804 } | 860 } |
805 | 861 |
806 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() { | 862 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() { |
807 } | 863 } |
808 | 864 |
809 std::vector<std::string> | 865 std::vector<std::string> |
810 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const { | 866 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const { |
811 std::vector<std::string> headers( | 867 std::vector<std::string> headers( |
812 MultipartUploadRequestBase::GetExtraRequestHeaders()); | 868 MultipartUploadRequestBase::GetExtraRequestHeaders()); |
813 headers.push_back(util::GenerateIfMatchHeader(etag_)); | 869 headers.push_back(util::GenerateIfMatchHeader(etag_)); |
814 return headers; | 870 return headers; |
815 } | 871 } |
816 | 872 |
817 GURL MultipartUploadExistingFileRequest::GetURL() const { | 873 GURL MultipartUploadExistingFileRequest::GetURL() const { |
818 return url_generator_.GetMultipartUploadExistingFileUrl( | 874 return url_generator_.GetMultipartUploadExistingFileUrl(resource_id_, |
819 resource_id_, has_modified_date()); | 875 has_modified_date_); |
820 } | 876 } |
821 | 877 |
822 net::URLFetcher::RequestType | 878 net::URLFetcher::RequestType |
823 MultipartUploadExistingFileRequest::GetRequestType() const { | 879 MultipartUploadExistingFileRequest::GetRequestType() const { |
824 return net::URLFetcher::PUT; | 880 return net::URLFetcher::PUT; |
825 } | 881 } |
826 | 882 |
827 //========================== DownloadFileRequest ========================== | 883 //========================== DownloadFileRequest ========================== |
828 | 884 |
829 DownloadFileRequest::DownloadFileRequest( | 885 DownloadFileRequest::DownloadFileRequest( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 } | 964 } |
909 break; | 965 break; |
910 } | 966 } |
911 root.SetString("value", value_); | 967 root.SetString("value", value_); |
912 base::JSONWriter::Write(&root, upload_content); | 968 base::JSONWriter::Write(&root, upload_content); |
913 return true; | 969 return true; |
914 } | 970 } |
915 | 971 |
916 } // namespace drive | 972 } // namespace drive |
917 } // namespace google_apis | 973 } // namespace google_apis |
OLD | NEW |