| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> |
| 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/callback.h" | 12 #include "base/callback.h" |
| 11 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 12 #include "base/location.h" | 14 #include "base/location.h" |
| 13 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
| 16 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
| 17 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 86 } |
| 85 | 87 |
| 86 // Attaches |properties| to the |request_body| if |properties| is not empty. | 88 // Attaches |properties| to the |request_body| if |properties| is not empty. |
| 87 // |request_body| must not be NULL. | 89 // |request_body| must not be NULL. |
| 88 void AttachProperties(const Properties& properties, | 90 void AttachProperties(const Properties& properties, |
| 89 base::DictionaryValue* request_body) { | 91 base::DictionaryValue* request_body) { |
| 90 DCHECK(request_body); | 92 DCHECK(request_body); |
| 91 if (properties.empty()) | 93 if (properties.empty()) |
| 92 return; | 94 return; |
| 93 | 95 |
| 94 base::ListValue* const properties_value = new base::ListValue; | 96 auto properties_value = base::MakeUnique<base::ListValue>(); |
| 95 for (const auto& property : properties) { | 97 for (const auto& property : properties) { |
| 96 std::unique_ptr<base::DictionaryValue> property_value( | 98 auto property_value = base::MakeUnique<base::DictionaryValue>(); |
| 97 new base::DictionaryValue); | |
| 98 std::string visibility_as_string; | 99 std::string visibility_as_string; |
| 99 switch (property.visibility()) { | 100 switch (property.visibility()) { |
| 100 case Property::VISIBILITY_PRIVATE: | 101 case Property::VISIBILITY_PRIVATE: |
| 101 visibility_as_string = "PRIVATE"; | 102 visibility_as_string = "PRIVATE"; |
| 102 break; | 103 break; |
| 103 case Property::VISIBILITY_PUBLIC: | 104 case Property::VISIBILITY_PUBLIC: |
| 104 visibility_as_string = "PUBLIC"; | 105 visibility_as_string = "PUBLIC"; |
| 105 break; | 106 break; |
| 106 } | 107 } |
| 107 property_value->SetString("visibility", visibility_as_string); | 108 property_value->SetString("visibility", visibility_as_string); |
| 108 property_value->SetString("key", property.key()); | 109 property_value->SetString("key", property.key()); |
| 109 property_value->SetString("value", property.value()); | 110 property_value->SetString("value", property.value()); |
| 110 properties_value->Append(std::move(property_value)); | 111 properties_value->Append(std::move(property_value)); |
| 111 } | 112 } |
| 112 request_body->Set("properties", properties_value); | 113 request_body->Set("properties", std::move(properties_value)); |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Creates metadata JSON string for multipart uploading. | 116 // Creates metadata JSON string for multipart uploading. |
| 116 // All the values are optional. If the value is empty or null, the value does | 117 // All the values are optional. If the value is empty or null, the value does |
| 117 // not appear in the metadata. | 118 // not appear in the metadata. |
| 118 std::string CreateMultipartUploadMetadataJson( | 119 std::string CreateMultipartUploadMetadataJson( |
| 119 const std::string& title, | 120 const std::string& title, |
| 120 const std::string& parent_resource_id, | 121 const std::string& parent_resource_id, |
| 121 const base::Time& modified_date, | 122 const base::Time& modified_date, |
| 122 const base::Time& last_viewed_by_me_date, | 123 const base::Time& last_viewed_by_me_date, |
| 123 const Properties& properties) { | 124 const Properties& properties) { |
| 124 base::DictionaryValue root; | 125 base::DictionaryValue root; |
| 125 if (!title.empty()) | 126 if (!title.empty()) |
| 126 root.SetString("title", title); | 127 root.SetString("title", title); |
| 127 | 128 |
| 128 // Fill parent link. | 129 // Fill parent link. |
| 129 if (!parent_resource_id.empty()) { | 130 if (!parent_resource_id.empty()) { |
| 130 std::unique_ptr<base::ListValue> parents(new base::ListValue); | 131 auto parents = base::MakeUnique<base::ListValue>(); |
| 131 parents->Append(google_apis::util::CreateParentValue(parent_resource_id)); | 132 parents->Append(google_apis::util::CreateParentValue(parent_resource_id)); |
| 132 root.Set("parents", parents.release()); | 133 root.Set("parents", std::move(parents)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 if (!modified_date.is_null()) { | 136 if (!modified_date.is_null()) { |
| 136 root.SetString("modifiedDate", | 137 root.SetString("modifiedDate", |
| 137 google_apis::util::FormatTimeAsString(modified_date)); | 138 google_apis::util::FormatTimeAsString(modified_date)); |
| 138 } | 139 } |
| 139 | 140 |
| 140 if (!last_viewed_by_me_date.is_null()) { | 141 if (!last_viewed_by_me_date.is_null()) { |
| 141 root.SetString("lastViewedByMeDate", google_apis::util::FormatTimeAsString( | 142 root.SetString("lastViewedByMeDate", google_apis::util::FormatTimeAsString( |
| 142 last_viewed_by_me_date)); | 143 last_viewed_by_me_date)); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 util::FormatTimeAsString(last_viewed_by_me_date_)); | 381 util::FormatTimeAsString(last_viewed_by_me_date_)); |
| 381 } | 382 } |
| 382 | 383 |
| 383 if (!mime_type_.empty()) | 384 if (!mime_type_.empty()) |
| 384 root.SetString("mimeType", mime_type_); | 385 root.SetString("mimeType", mime_type_); |
| 385 | 386 |
| 386 if (!modified_date_.is_null()) | 387 if (!modified_date_.is_null()) |
| 387 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 388 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 388 | 389 |
| 389 if (!parents_.empty()) { | 390 if (!parents_.empty()) { |
| 390 base::ListValue* parents_value = new base::ListValue; | 391 auto parents_value = base::MakeUnique<base::ListValue>(); |
| 391 for (size_t i = 0; i < parents_.size(); ++i) { | 392 for (size_t i = 0; i < parents_.size(); ++i) { |
| 392 std::unique_ptr<base::DictionaryValue> parent(new base::DictionaryValue); | 393 auto parent = base::MakeUnique<base::DictionaryValue>(); |
| 393 parent->SetString("id", parents_[i]); | 394 parent->SetString("id", parents_[i]); |
| 394 parents_value->Append(std::move(parent)); | 395 parents_value->Append(std::move(parent)); |
| 395 } | 396 } |
| 396 root.Set("parents", parents_value); | 397 root.Set("parents", std::move(parents_value)); |
| 397 } | 398 } |
| 398 | 399 |
| 399 if (!title_.empty()) | 400 if (!title_.empty()) |
| 400 root.SetString("title", title_); | 401 root.SetString("title", title_); |
| 401 | 402 |
| 402 AttachProperties(properties_, &root); | 403 AttachProperties(properties_, &root); |
| 403 base::JSONWriter::Write(root, upload_content); | 404 base::JSONWriter::Write(root, upload_content); |
| 404 | 405 |
| 405 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", [" | 406 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", [" |
| 406 << *upload_content << "]"; | 407 << *upload_content << "]"; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 | 459 |
| 459 if (!modified_date_.is_null()) | 460 if (!modified_date_.is_null()) |
| 460 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 461 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 461 | 462 |
| 462 if (!last_viewed_by_me_date_.is_null()) { | 463 if (!last_viewed_by_me_date_.is_null()) { |
| 463 root.SetString("lastViewedByMeDate", | 464 root.SetString("lastViewedByMeDate", |
| 464 util::FormatTimeAsString(last_viewed_by_me_date_)); | 465 util::FormatTimeAsString(last_viewed_by_me_date_)); |
| 465 } | 466 } |
| 466 | 467 |
| 467 if (!parents_.empty()) { | 468 if (!parents_.empty()) { |
| 468 base::ListValue* parents_value = new base::ListValue; | 469 auto parents_value = base::MakeUnique<base::ListValue>(); |
| 469 for (size_t i = 0; i < parents_.size(); ++i) { | 470 for (size_t i = 0; i < parents_.size(); ++i) { |
| 470 std::unique_ptr<base::DictionaryValue> parent(new base::DictionaryValue); | 471 auto parent = base::MakeUnique<base::DictionaryValue>(); |
| 471 parent->SetString("id", parents_[i]); | 472 parent->SetString("id", parents_[i]); |
| 472 parents_value->Append(std::move(parent)); | 473 parents_value->Append(std::move(parent)); |
| 473 } | 474 } |
| 474 root.Set("parents", parents_value); | 475 root.Set("parents", std::move(parents_value)); |
| 475 } | 476 } |
| 476 | 477 |
| 477 AttachProperties(properties_, &root); | 478 AttachProperties(properties_, &root); |
| 478 base::JSONWriter::Write(root, upload_content); | 479 base::JSONWriter::Write(root, upload_content); |
| 479 | 480 |
| 480 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" | 481 DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", [" |
| 481 << *upload_content << "]"; | 482 << *upload_content << "]"; |
| 482 return true; | 483 return true; |
| 483 } | 484 } |
| 484 | 485 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 512 return false; | 513 return false; |
| 513 | 514 |
| 514 *upload_content_type = util::kContentTypeApplicationJson; | 515 *upload_content_type = util::kContentTypeApplicationJson; |
| 515 | 516 |
| 516 base::DictionaryValue root; | 517 base::DictionaryValue root; |
| 517 | 518 |
| 518 if (!modified_date_.is_null()) | 519 if (!modified_date_.is_null()) |
| 519 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 520 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 520 | 521 |
| 521 if (!parents_.empty()) { | 522 if (!parents_.empty()) { |
| 522 base::ListValue* parents_value = new base::ListValue; | 523 auto parents_value = base::MakeUnique<base::ListValue>(); |
| 523 for (size_t i = 0; i < parents_.size(); ++i) { | 524 for (size_t i = 0; i < parents_.size(); ++i) { |
| 524 std::unique_ptr<base::DictionaryValue> parent(new base::DictionaryValue); | 525 auto parent = base::MakeUnique<base::DictionaryValue>(); |
| 525 parent->SetString("id", parents_[i]); | 526 parent->SetString("id", parents_[i]); |
| 526 parents_value->Append(std::move(parent)); | 527 parents_value->Append(std::move(parent)); |
| 527 } | 528 } |
| 528 root.Set("parents", parents_value); | 529 root.Set("parents", std::move(parents_value)); |
| 529 } | 530 } |
| 530 | 531 |
| 531 if (!title_.empty()) | 532 if (!title_.empty()) |
| 532 root.SetString("title", title_); | 533 root.SetString("title", title_); |
| 533 | 534 |
| 534 base::JSONWriter::Write(root, upload_content); | 535 base::JSONWriter::Write(root, upload_content); |
| 535 DVLOG(1) << "FilesCopy data: " << *upload_content_type << ", [" | 536 DVLOG(1) << "FilesCopy data: " << *upload_content_type << ", [" |
| 536 << *upload_content << "]"; | 537 << *upload_content << "]"; |
| 537 return true; | 538 return true; |
| 538 } | 539 } |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 816 | 817 |
| 817 bool InitiateUploadNewFileRequest::GetContentData( | 818 bool InitiateUploadNewFileRequest::GetContentData( |
| 818 std::string* upload_content_type, | 819 std::string* upload_content_type, |
| 819 std::string* upload_content) { | 820 std::string* upload_content) { |
| 820 *upload_content_type = util::kContentTypeApplicationJson; | 821 *upload_content_type = util::kContentTypeApplicationJson; |
| 821 | 822 |
| 822 base::DictionaryValue root; | 823 base::DictionaryValue root; |
| 823 root.SetString("title", title_); | 824 root.SetString("title", title_); |
| 824 | 825 |
| 825 // Fill parent link. | 826 // Fill parent link. |
| 826 std::unique_ptr<base::ListValue> parents(new base::ListValue); | 827 auto parents = base::MakeUnique<base::ListValue>(); |
| 827 parents->Append(util::CreateParentValue(parent_resource_id_)); | 828 parents->Append(util::CreateParentValue(parent_resource_id_)); |
| 828 root.Set("parents", parents.release()); | 829 root.Set("parents", std::move(parents)); |
| 829 | 830 |
| 830 if (!modified_date_.is_null()) | 831 if (!modified_date_.is_null()) |
| 831 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 832 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 832 | 833 |
| 833 if (!last_viewed_by_me_date_.is_null()) { | 834 if (!last_viewed_by_me_date_.is_null()) { |
| 834 root.SetString("lastViewedByMeDate", | 835 root.SetString("lastViewedByMeDate", |
| 835 util::FormatTimeAsString(last_viewed_by_me_date_)); | 836 util::FormatTimeAsString(last_viewed_by_me_date_)); |
| 836 } | 837 } |
| 837 | 838 |
| 838 AttachProperties(properties_, &root); | 839 AttachProperties(properties_, &root); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 InitiateUploadRequestBase::GetExtraRequestHeaders()); | 877 InitiateUploadRequestBase::GetExtraRequestHeaders()); |
| 877 headers.push_back(util::GenerateIfMatchHeader(etag_)); | 878 headers.push_back(util::GenerateIfMatchHeader(etag_)); |
| 878 return headers; | 879 return headers; |
| 879 } | 880 } |
| 880 | 881 |
| 881 bool InitiateUploadExistingFileRequest::GetContentData( | 882 bool InitiateUploadExistingFileRequest::GetContentData( |
| 882 std::string* upload_content_type, | 883 std::string* upload_content_type, |
| 883 std::string* upload_content) { | 884 std::string* upload_content) { |
| 884 base::DictionaryValue root; | 885 base::DictionaryValue root; |
| 885 if (!parent_resource_id_.empty()) { | 886 if (!parent_resource_id_.empty()) { |
| 886 std::unique_ptr<base::ListValue> parents(new base::ListValue); | 887 auto parents = base::MakeUnique<base::ListValue>(); |
| 887 parents->Append(util::CreateParentValue(parent_resource_id_)); | 888 parents->Append(util::CreateParentValue(parent_resource_id_)); |
| 888 root.Set("parents", parents.release()); | 889 root.Set("parents", std::move(parents)); |
| 889 } | 890 } |
| 890 | 891 |
| 891 if (!title_.empty()) | 892 if (!title_.empty()) |
| 892 root.SetString("title", title_); | 893 root.SetString("title", title_); |
| 893 | 894 |
| 894 if (!modified_date_.is_null()) | 895 if (!modified_date_.is_null()) |
| 895 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); | 896 root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_)); |
| 896 | 897 |
| 897 if (!last_viewed_by_me_date_.is_null()) { | 898 if (!last_viewed_by_me_date_.is_null()) { |
| 898 root.SetString("lastViewedByMeDate", | 899 root.SetString("lastViewedByMeDate", |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1140 break; | 1141 break; |
| 1141 case PERMISSION_ROLE_READER: | 1142 case PERMISSION_ROLE_READER: |
| 1142 root.SetString("role", "reader"); | 1143 root.SetString("role", "reader"); |
| 1143 break; | 1144 break; |
| 1144 case PERMISSION_ROLE_WRITER: | 1145 case PERMISSION_ROLE_WRITER: |
| 1145 root.SetString("role", "writer"); | 1146 root.SetString("role", "writer"); |
| 1146 break; | 1147 break; |
| 1147 case PERMISSION_ROLE_COMMENTER: | 1148 case PERMISSION_ROLE_COMMENTER: |
| 1148 root.SetString("role", "reader"); | 1149 root.SetString("role", "reader"); |
| 1149 { | 1150 { |
| 1150 base::ListValue* list = new base::ListValue; | 1151 auto list = base::MakeUnique<base::ListValue>(); |
| 1151 list->AppendString("commenter"); | 1152 list->AppendString("commenter"); |
| 1152 root.Set("additionalRoles", list); | 1153 root.Set("additionalRoles", std::move(list)); |
| 1153 } | 1154 } |
| 1154 break; | 1155 break; |
| 1155 } | 1156 } |
| 1156 root.SetString("value", value_); | 1157 root.SetString("value", value_); |
| 1157 base::JSONWriter::Write(root, upload_content); | 1158 base::JSONWriter::Write(root, upload_content); |
| 1158 return true; | 1159 return true; |
| 1159 } | 1160 } |
| 1160 | 1161 |
| 1161 //======================= SingleBatchableDelegateRequest ======================= | 1162 //======================= SingleBatchableDelegateRequest ======================= |
| 1162 | 1163 |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1446 } else if (last_progress_value_ < child->data_offset + child->data_size && | 1447 } else if (last_progress_value_ < child->data_offset + child->data_size && |
| 1447 child->data_offset + child->data_size < current) { | 1448 child->data_offset + child->data_size < current) { |
| 1448 child->request->NotifyUploadProgress(source, child->data_size, | 1449 child->request->NotifyUploadProgress(source, child->data_size, |
| 1449 child->data_size); | 1450 child->data_size); |
| 1450 } | 1451 } |
| 1451 } | 1452 } |
| 1452 last_progress_value_ = current; | 1453 last_progress_value_ = current; |
| 1453 } | 1454 } |
| 1454 } // namespace drive | 1455 } // namespace drive |
| 1455 } // namespace google_apis | 1456 } // namespace google_apis |
| OLD | NEW |