Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: google_apis/drive/drive_api_requests.cc

Issue 944413003: Add support for setting properties to requests uploading contents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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(
mtomasz 2015/02/23 09:12:52 This is copy paste from base_requests.cc + added A
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 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 if (!parents_.empty()) { 287 if (!parents_.empty()) {
226 base::ListValue* parents_value = new base::ListValue; 288 base::ListValue* parents_value = new base::ListValue;
227 for (size_t i = 0; i < parents_.size(); ++i) { 289 for (size_t i = 0; i < parents_.size(); ++i) {
228 base::DictionaryValue* parent = new base::DictionaryValue; 290 base::DictionaryValue* parent = new base::DictionaryValue;
229 parent->SetString("id", parents_[i]); 291 parent->SetString("id", parents_[i]);
230 parents_value->Append(parent); 292 parents_value->Append(parent);
231 } 293 }
232 root.Set("parents", parents_value); 294 root.Set("parents", parents_value);
233 } 295 }
234 296
235 if (!properties_.empty()) { 297 AttachProperties(properties_, &root);
236 base::ListValue* properties_value = new base::ListValue; 298 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 299
256 base::JSONWriter::Write(&root, upload_content);
257 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" 300 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", ["
258 << *upload_content << "]"; 301 << *upload_content << "]";
259 return true; 302 return true;
260 } 303 }
261 304
262 //============================= FilesCopyRequest ============================== 305 //============================= FilesCopyRequest ==============================
263 306
264 FilesCopyRequest::FilesCopyRequest( 307 FilesCopyRequest::FilesCopyRequest(
265 RequestSender* sender, 308 RequestSender* sender,
266 const DriveApiUrlGenerator& url_generator, 309 const DriveApiUrlGenerator& url_generator,
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 root.Set("parents", parents.release()); 630 root.Set("parents", parents.release());
588 631
589 if (!modified_date_.is_null()) 632 if (!modified_date_.is_null())
590 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); 633 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_));
591 634
592 if (!last_viewed_by_me_date_.is_null()) { 635 if (!last_viewed_by_me_date_.is_null()) {
593 root.SetString("lastViewedByMeDate", 636 root.SetString("lastViewedByMeDate",
594 util::FormatTimeAsString(last_viewed_by_me_date_)); 637 util::FormatTimeAsString(last_viewed_by_me_date_));
595 } 638 }
596 639
640 AttachProperties(properties_, &root);
597 base::JSONWriter::Write(&root, upload_content); 641 base::JSONWriter::Write(&root, upload_content);
598 642
599 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", [" 643 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", ["
600 << *upload_content << "]"; 644 << *upload_content << "]";
601 return true; 645 return true;
602 } 646 }
603 647
604 //===================== InitiateUploadExistingFileRequest ==================== 648 //===================== InitiateUploadExistingFileRequest ====================
605 649
606 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest( 650 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 root.SetString("title", title_); 698 root.SetString("title", title_);
655 699
656 if (!modified_date_.is_null()) 700 if (!modified_date_.is_null())
657 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); 701 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_));
658 702
659 if (!last_viewed_by_me_date_.is_null()) { 703 if (!last_viewed_by_me_date_.is_null()) {
660 root.SetString("lastViewedByMeDate", 704 root.SetString("lastViewedByMeDate",
661 util::FormatTimeAsString(last_viewed_by_me_date_)); 705 util::FormatTimeAsString(last_viewed_by_me_date_));
662 } 706 }
663 707
708 AttachProperties(properties_, &root);
664 if (root.empty()) 709 if (root.empty())
665 return false; 710 return false;
666 711
667 *upload_content_type = util::kContentTypeApplicationJson; 712 *upload_content_type = util::kContentTypeApplicationJson;
668 base::JSONWriter::Write(&root, upload_content); 713 base::JSONWriter::Write(&root, upload_content);
669 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type 714 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type
670 << ", [" << *upload_content << "]"; 715 << ", [" << *upload_content << "]";
671 return true; 716 return true;
672 } 717 }
673 718
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 782
738 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest( 783 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest(
739 RequestSender* sender, 784 RequestSender* sender,
740 const std::string& title, 785 const std::string& title,
741 const std::string& parent_resource_id, 786 const std::string& parent_resource_id,
742 const std::string& content_type, 787 const std::string& content_type,
743 int64 content_length, 788 int64 content_length,
744 const base::Time& modified_date, 789 const base::Time& modified_date,
745 const base::Time& last_viewed_by_me_date, 790 const base::Time& last_viewed_by_me_date,
746 const base::FilePath& local_file_path, 791 const base::FilePath& local_file_path,
792 const Properties& properties,
747 const DriveApiUrlGenerator& url_generator, 793 const DriveApiUrlGenerator& url_generator,
748 const FileResourceCallback& callback, 794 const FileResourceCallback& callback,
749 const ProgressCallback& progress_callback) 795 const ProgressCallback& progress_callback)
750 : MultipartUploadRequestBase(sender, 796 : MultipartUploadRequestBase(
751 title, 797 sender,
752 parent_resource_id, 798 CreateMultipartUploadMetadataJson(title,
753 content_type, 799 parent_resource_id,
754 content_length, 800 modified_date,
755 modified_date, 801 last_viewed_by_me_date,
756 last_viewed_by_me_date, 802 properties),
757 local_file_path, 803 content_type,
758 callback, 804 content_length,
759 progress_callback), 805 local_file_path,
806 callback,
807 progress_callback),
808 has_modified_date_(!modified_date.is_null()),
760 url_generator_(url_generator) { 809 url_generator_(url_generator) {
761 } 810 }
762 811
763 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() { 812 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() {
764 } 813 }
765 814
766 GURL MultipartUploadNewFileRequest::GetURL() const { 815 GURL MultipartUploadNewFileRequest::GetURL() const {
767 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date()); 816 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date_);
768 } 817 }
769 818
770 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType() 819 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType()
771 const { 820 const {
772 return net::URLFetcher::POST; 821 return net::URLFetcher::POST;
773 } 822 }
774 823
775 //======================= MultipartUploadExistingFileRequest =================== 824 //======================= MultipartUploadExistingFileRequest ===================
776 825
777 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest( 826 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest(
778 RequestSender* sender, 827 RequestSender* sender,
779 const std::string& title, 828 const std::string& title,
780 const std::string& resource_id, 829 const std::string& resource_id,
781 const std::string& parent_resource_id, 830 const std::string& parent_resource_id,
782 const std::string& content_type, 831 const std::string& content_type,
783 int64 content_length, 832 int64 content_length,
784 const base::Time& modified_date, 833 const base::Time& modified_date,
785 const base::Time& last_viewed_by_me_date, 834 const base::Time& last_viewed_by_me_date,
786 const base::FilePath& local_file_path, 835 const base::FilePath& local_file_path,
787 const std::string& etag, 836 const std::string& etag,
837 const Properties& properties,
788 const DriveApiUrlGenerator& url_generator, 838 const DriveApiUrlGenerator& url_generator,
789 const FileResourceCallback& callback, 839 const FileResourceCallback& callback,
790 const ProgressCallback& progress_callback) 840 const ProgressCallback& progress_callback)
791 : MultipartUploadRequestBase(sender, 841 : MultipartUploadRequestBase(
792 title, 842 sender,
793 parent_resource_id, 843 CreateMultipartUploadMetadataJson(title,
794 content_type, 844 parent_resource_id,
795 content_length, 845 modified_date,
796 modified_date, 846 last_viewed_by_me_date,
797 last_viewed_by_me_date, 847 properties),
798 local_file_path, 848 content_type,
799 callback, 849 content_length,
800 progress_callback), 850 local_file_path,
851 callback,
852 progress_callback),
801 resource_id_(resource_id), 853 resource_id_(resource_id),
802 etag_(etag), 854 etag_(etag),
855 has_modified_date_(!modified_date.is_null()),
803 url_generator_(url_generator) { 856 url_generator_(url_generator) {
804 } 857 }
805 858
806 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() { 859 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() {
807 } 860 }
808 861
809 std::vector<std::string> 862 std::vector<std::string>
810 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const { 863 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const {
811 std::vector<std::string> headers( 864 std::vector<std::string> headers(
812 MultipartUploadRequestBase::GetExtraRequestHeaders()); 865 MultipartUploadRequestBase::GetExtraRequestHeaders());
813 headers.push_back(util::GenerateIfMatchHeader(etag_)); 866 headers.push_back(util::GenerateIfMatchHeader(etag_));
814 return headers; 867 return headers;
815 } 868 }
816 869
817 GURL MultipartUploadExistingFileRequest::GetURL() const { 870 GURL MultipartUploadExistingFileRequest::GetURL() const {
818 return url_generator_.GetMultipartUploadExistingFileUrl( 871 return url_generator_.GetMultipartUploadExistingFileUrl(resource_id_,
819 resource_id_, has_modified_date()); 872 has_modified_date_);
820 } 873 }
821 874
822 net::URLFetcher::RequestType 875 net::URLFetcher::RequestType
823 MultipartUploadExistingFileRequest::GetRequestType() const { 876 MultipartUploadExistingFileRequest::GetRequestType() const {
824 return net::URLFetcher::PUT; 877 return net::URLFetcher::PUT;
825 } 878 }
826 879
827 //========================== DownloadFileRequest ========================== 880 //========================== DownloadFileRequest ==========================
828 881
829 DownloadFileRequest::DownloadFileRequest( 882 DownloadFileRequest::DownloadFileRequest(
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 } 961 }
909 break; 962 break;
910 } 963 }
911 root.SetString("value", value_); 964 root.SetString("value", value_);
912 base::JSONWriter::Write(&root, upload_content); 965 base::JSONWriter::Write(&root, upload_content);
913 return true; 966 return true;
914 } 967 }
915 968
916 } // namespace drive 969 } // namespace drive
917 } // namespace google_apis 970 } // namespace google_apis
OLDNEW
« no previous file with comments | « google_apis/drive/drive_api_requests.h ('k') | google_apis/drive/drive_api_requests_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698