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_parser.h" | 5 #include "google_apis/drive/drive_api_parser.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace { | 21 namespace { |
22 | 22 |
23 const int64_t kUnsetFileSize = -1; | 23 const int64_t kUnsetFileSize = -1; |
24 | 24 |
25 bool CreateFileResourceFromValue(const base::Value* value, | 25 bool CreateFileResourceFromValue(const base::Value* value, |
26 std::unique_ptr<FileResource>* file) { | 26 std::unique_ptr<FileResource>* file) { |
27 *file = FileResource::CreateFrom(*value); | 27 *file = FileResource::CreateFrom(*value); |
28 return !!*file; | 28 return !!*file; |
29 } | 29 } |
30 | 30 |
| 31 bool CreateTeamDriveResourceFromValue( |
| 32 const base::Value* value, |
| 33 std::unique_ptr<TeamDriveResource>* file) { |
| 34 *file = TeamDriveResource::CreateFrom(*value); |
| 35 return !!*file; |
| 36 } |
| 37 |
31 // Converts |url_string| to |result|. Always returns true to be used | 38 // Converts |url_string| to |result|. Always returns true to be used |
32 // for JSONValueConverter::RegisterCustomField method. | 39 // for JSONValueConverter::RegisterCustomField method. |
33 // TODO(mukai): make it return false in case of invalid |url_string|. | 40 // TODO(mukai): make it return false in case of invalid |url_string|. |
34 bool GetGURLFromString(const base::StringPiece& url_string, GURL* result) { | 41 bool GetGURLFromString(const base::StringPiece& url_string, GURL* result) { |
35 *result = GURL(url_string); | 42 *result = GURL(url_string); |
36 return true; | 43 return true; |
37 } | 44 } |
38 | 45 |
39 // Converts |value| to |result|. | 46 // Converts |value| to |result|. |
40 bool GetParentsFromValue(const base::Value* value, | 47 bool GetParentsFromValue(const base::Value* value, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 const char kCanShare[] = "canShare"; | 191 const char kCanShare[] = "canShare"; |
185 | 192 |
186 // Files List | 193 // Files List |
187 // https://developers.google.com/drive/v2/reference/files/list | 194 // https://developers.google.com/drive/v2/reference/files/list |
188 const char kFileListKind[] = "drive#fileList"; | 195 const char kFileListKind[] = "drive#fileList"; |
189 const char kNextLink[] = "nextLink"; | 196 const char kNextLink[] = "nextLink"; |
190 | 197 |
191 // Change Resource | 198 // Change Resource |
192 // https://developers.google.com/drive/v2/reference/changes | 199 // https://developers.google.com/drive/v2/reference/changes |
193 const char kChangeKind[] = "drive#change"; | 200 const char kChangeKind[] = "drive#change"; |
| 201 const char kType[] = "type"; |
194 const char kFileId[] = "fileId"; | 202 const char kFileId[] = "fileId"; |
195 const char kDeleted[] = "deleted"; | 203 const char kDeleted[] = "deleted"; |
196 const char kFile[] = "file"; | 204 const char kFile[] = "file"; |
| 205 const char kTeamDrive[] = "teamDrive"; |
| 206 const char kTeamDriveId[] = "teamDriveId"; |
197 | 207 |
198 // Changes List | 208 // Changes List |
199 // https://developers.google.com/drive/v2/reference/changes/list | 209 // https://developers.google.com/drive/v2/reference/changes/list |
200 const char kChangeListKind[] = "drive#changeList"; | 210 const char kChangeListKind[] = "drive#changeList"; |
201 | 211 |
| 212 // Maps category name to enum ChangeType. |
| 213 struct ChangeTypeMap { |
| 214 ChangeResource::ChangeType type; |
| 215 const char* type_name; |
| 216 }; |
| 217 |
| 218 constexpr ChangeTypeMap kChangeTypeMap[] = { |
| 219 { ChangeResource::FILE, "file" }, |
| 220 { ChangeResource::TEAM_DRIVE, "teamDrive" }, |
| 221 }; |
| 222 |
202 // Maps category name to enum IconCategory. | 223 // Maps category name to enum IconCategory. |
203 struct AppIconCategoryMap { | 224 struct AppIconCategoryMap { |
204 DriveAppIcon::IconCategory category; | 225 DriveAppIcon::IconCategory category; |
205 const char* category_name; | 226 const char* category_name; |
206 }; | 227 }; |
207 | 228 |
208 const AppIconCategoryMap kAppIconCategoryMap[] = { | 229 constexpr AppIconCategoryMap kAppIconCategoryMap[] = { |
209 { DriveAppIcon::DOCUMENT, "document" }, | 230 { DriveAppIcon::DOCUMENT, "document" }, |
210 { DriveAppIcon::APPLICATION, "application" }, | 231 { DriveAppIcon::APPLICATION, "application" }, |
211 { DriveAppIcon::SHARED_DOCUMENT, "documentShared" }, | 232 { DriveAppIcon::SHARED_DOCUMENT, "documentShared" }, |
212 }; | 233 }; |
213 | 234 |
214 // Checks if the JSON is expected kind. In Drive API, JSON data structure has | 235 // Checks if the JSON is expected kind. In Drive API, JSON data structure has |
215 // |kind| property which denotes the type of the structure (e.g. "drive#file"). | 236 // |kind| property which denotes the type of the structure (e.g. "drive#file"). |
216 bool IsResourceKindExpected(const base::Value& value, | 237 bool IsResourceKindExpected(const base::Value& value, |
217 const std::string& expected_kind) { | 238 const std::string& expected_kind) { |
218 const base::DictionaryValue* as_dict = NULL; | 239 const base::DictionaryValue* as_dict = NULL; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 can_edit_(false), | 443 can_edit_(false), |
423 can_list_children_(false), | 444 can_list_children_(false), |
424 can_manage_members_(false), | 445 can_manage_members_(false), |
425 can_read_revisions_(false), | 446 can_read_revisions_(false), |
426 can_remove_children_(false), | 447 can_remove_children_(false), |
427 can_rename_(false), | 448 can_rename_(false), |
428 can_rename_team_drive_(false), | 449 can_rename_team_drive_(false), |
429 can_share_(false) { | 450 can_share_(false) { |
430 } | 451 } |
431 | 452 |
| 453 TeamDriveCapabilities::TeamDriveCapabilities(const TeamDriveCapabilities& src) = |
| 454 default; |
| 455 |
432 TeamDriveCapabilities::~TeamDriveCapabilities(){} | 456 TeamDriveCapabilities::~TeamDriveCapabilities(){} |
433 | 457 |
434 // static | 458 // static |
435 void TeamDriveCapabilities::RegisterJSONConverter( | 459 void TeamDriveCapabilities::RegisterJSONConverter( |
436 base::JSONValueConverter<TeamDriveCapabilities>* converter) { | 460 base::JSONValueConverter<TeamDriveCapabilities>* converter) { |
437 converter->RegisterBoolField(kCanAddChildren, | 461 converter->RegisterBoolField(kCanAddChildren, |
438 &TeamDriveCapabilities::can_add_children_); | 462 &TeamDriveCapabilities::can_add_children_); |
439 converter->RegisterBoolField(kCanComment, | 463 converter->RegisterBoolField(kCanComment, |
440 &TeamDriveCapabilities::can_comment_); | 464 &TeamDriveCapabilities::can_comment_); |
441 converter->RegisterBoolField(kCanCopy, &TeamDriveCapabilities::can_copy_); | 465 converter->RegisterBoolField(kCanCopy, &TeamDriveCapabilities::can_copy_); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 if (!converter.Convert(value, this)) { | 675 if (!converter.Convert(value, this)) { |
652 LOG(ERROR) << "Unable to parse: Invalid FileList"; | 676 LOG(ERROR) << "Unable to parse: Invalid FileList"; |
653 return false; | 677 return false; |
654 } | 678 } |
655 return true; | 679 return true; |
656 } | 680 } |
657 | 681 |
658 //////////////////////////////////////////////////////////////////////////////// | 682 //////////////////////////////////////////////////////////////////////////////// |
659 // ChangeResource implementation | 683 // ChangeResource implementation |
660 | 684 |
661 ChangeResource::ChangeResource() : change_id_(0), deleted_(false) {} | 685 ChangeResource::ChangeResource() |
| 686 : change_id_(0), type_(UNKNOWN), deleted_(false) {} |
662 | 687 |
663 ChangeResource::~ChangeResource() {} | 688 ChangeResource::~ChangeResource() {} |
664 | 689 |
665 // static | 690 // static |
666 void ChangeResource::RegisterJSONConverter( | 691 void ChangeResource::RegisterJSONConverter( |
667 base::JSONValueConverter<ChangeResource>* converter) { | 692 base::JSONValueConverter<ChangeResource>* converter) { |
668 converter->RegisterCustomField<int64_t>(kId, &ChangeResource::change_id_, | 693 converter->RegisterCustomField<int64_t>(kId, &ChangeResource::change_id_, |
669 &base::StringToInt64); | 694 &base::StringToInt64); |
| 695 converter->RegisterCustomField<ChangeType>(kType, &ChangeResource::type_, |
| 696 &ChangeResource::GetType); |
670 converter->RegisterStringField(kFileId, &ChangeResource::file_id_); | 697 converter->RegisterStringField(kFileId, &ChangeResource::file_id_); |
671 converter->RegisterBoolField(kDeleted, &ChangeResource::deleted_); | 698 converter->RegisterBoolField(kDeleted, &ChangeResource::deleted_); |
672 converter->RegisterCustomValueField(kFile, &ChangeResource::file_, | 699 converter->RegisterCustomValueField(kFile, &ChangeResource::file_, |
673 &CreateFileResourceFromValue); | 700 &CreateFileResourceFromValue); |
674 converter->RegisterCustomField<base::Time>( | 701 converter->RegisterCustomField<base::Time>( |
675 kModificationDate, &ChangeResource::modification_date_, | 702 kModificationDate, &ChangeResource::modification_date_, |
676 &util::GetTimeFromString); | 703 &util::GetTimeFromString); |
| 704 converter->RegisterStringField(kTeamDriveId, &ChangeResource::team_drive_id_); |
| 705 converter->RegisterCustomValueField(kTeamDrive, &ChangeResource::team_drive_, |
| 706 &CreateTeamDriveResourceFromValue); |
677 } | 707 } |
678 | 708 |
679 // static | 709 // static |
680 std::unique_ptr<ChangeResource> ChangeResource::CreateFrom( | 710 std::unique_ptr<ChangeResource> ChangeResource::CreateFrom( |
681 const base::Value& value) { | 711 const base::Value& value) { |
682 std::unique_ptr<ChangeResource> resource(new ChangeResource()); | 712 std::unique_ptr<ChangeResource> resource(new ChangeResource()); |
683 if (!IsResourceKindExpected(value, kChangeKind) || !resource->Parse(value)) { | 713 if (!IsResourceKindExpected(value, kChangeKind) || !resource->Parse(value)) { |
684 LOG(ERROR) << "Unable to create: Invalid ChangeResource JSON!"; | 714 LOG(ERROR) << "Unable to create: Invalid ChangeResource JSON!"; |
685 return std::unique_ptr<ChangeResource>(); | 715 return std::unique_ptr<ChangeResource>(); |
686 } | 716 } |
687 return resource; | 717 return resource; |
688 } | 718 } |
689 | 719 |
690 bool ChangeResource::Parse(const base::Value& value) { | 720 bool ChangeResource::Parse(const base::Value& value) { |
691 base::JSONValueConverter<ChangeResource> converter; | 721 base::JSONValueConverter<ChangeResource> converter; |
692 if (!converter.Convert(value, this)) { | 722 if (!converter.Convert(value, this)) { |
693 LOG(ERROR) << "Unable to parse: Invalid ChangeResource"; | 723 LOG(ERROR) << "Unable to parse: Invalid ChangeResource"; |
694 return false; | 724 return false; |
695 } | 725 } |
696 return true; | 726 return true; |
697 } | 727 } |
698 | 728 |
| 729 // static |
| 730 bool ChangeResource::GetType(const base::StringPiece& type_name, |
| 731 ChangeResource::ChangeType* result) { |
| 732 for (size_t i = 0; i < arraysize(kChangeTypeMap); i++) { |
| 733 if (type_name == kChangeTypeMap[i].type_name) { |
| 734 *result = kChangeTypeMap[i].type; |
| 735 return true; |
| 736 } |
| 737 } |
| 738 DVLOG(1) << "Unknown change type" << type_name; |
| 739 return false; |
| 740 } |
| 741 |
699 //////////////////////////////////////////////////////////////////////////////// | 742 //////////////////////////////////////////////////////////////////////////////// |
700 // ChangeList implementation | 743 // ChangeList implementation |
701 | 744 |
702 ChangeList::ChangeList() : largest_change_id_(0) {} | 745 ChangeList::ChangeList() : largest_change_id_(0) {} |
703 | 746 |
704 ChangeList::~ChangeList() {} | 747 ChangeList::~ChangeList() {} |
705 | 748 |
706 // static | 749 // static |
707 void ChangeList::RegisterJSONConverter( | 750 void ChangeList::RegisterJSONConverter( |
708 base::JSONValueConverter<ChangeList>* converter) { | 751 base::JSONValueConverter<ChangeList>* converter) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 bool ImageMediaMetadata::Parse(const base::Value& value) { | 853 bool ImageMediaMetadata::Parse(const base::Value& value) { |
811 base::JSONValueConverter<ImageMediaMetadata> converter; | 854 base::JSONValueConverter<ImageMediaMetadata> converter; |
812 if (!converter.Convert(value, this)) { | 855 if (!converter.Convert(value, this)) { |
813 LOG(ERROR) << "Unable to parse: Invalid ImageMediaMetadata."; | 856 LOG(ERROR) << "Unable to parse: Invalid ImageMediaMetadata."; |
814 return false; | 857 return false; |
815 } | 858 } |
816 return true; | 859 return true; |
817 } | 860 } |
818 | 861 |
819 } // namespace google_apis | 862 } // namespace google_apis |
OLD | NEW |