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 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 if (!converter.Convert(value, this)) { | 672 if (!converter.Convert(value, this)) { |
652 LOG(ERROR) << "Unable to parse: Invalid FileList"; | 673 LOG(ERROR) << "Unable to parse: Invalid FileList"; |
653 return false; | 674 return false; |
654 } | 675 } |
655 return true; | 676 return true; |
656 } | 677 } |
657 | 678 |
658 //////////////////////////////////////////////////////////////////////////////// | 679 //////////////////////////////////////////////////////////////////////////////// |
659 // ChangeResource implementation | 680 // ChangeResource implementation |
660 | 681 |
661 ChangeResource::ChangeResource() : change_id_(0), deleted_(false) {} | 682 ChangeResource::ChangeResource() |
| 683 : change_id_(0), type_(UNKNOWN), deleted_(false) {} |
662 | 684 |
663 ChangeResource::~ChangeResource() {} | 685 ChangeResource::~ChangeResource() {} |
664 | 686 |
665 // static | 687 // static |
666 void ChangeResource::RegisterJSONConverter( | 688 void ChangeResource::RegisterJSONConverter( |
667 base::JSONValueConverter<ChangeResource>* converter) { | 689 base::JSONValueConverter<ChangeResource>* converter) { |
668 converter->RegisterCustomField<int64_t>(kId, &ChangeResource::change_id_, | 690 converter->RegisterCustomField<int64_t>(kId, &ChangeResource::change_id_, |
669 &base::StringToInt64); | 691 &base::StringToInt64); |
| 692 converter->RegisterCustomField<ChangeType>(kType, &ChangeResource::type_, |
| 693 &ChangeResource::GetType); |
670 converter->RegisterStringField(kFileId, &ChangeResource::file_id_); | 694 converter->RegisterStringField(kFileId, &ChangeResource::file_id_); |
671 converter->RegisterBoolField(kDeleted, &ChangeResource::deleted_); | 695 converter->RegisterBoolField(kDeleted, &ChangeResource::deleted_); |
672 converter->RegisterCustomValueField(kFile, &ChangeResource::file_, | 696 converter->RegisterCustomValueField(kFile, &ChangeResource::file_, |
673 &CreateFileResourceFromValue); | 697 &CreateFileResourceFromValue); |
674 converter->RegisterCustomField<base::Time>( | 698 converter->RegisterCustomField<base::Time>( |
675 kModificationDate, &ChangeResource::modification_date_, | 699 kModificationDate, &ChangeResource::modification_date_, |
676 &util::GetTimeFromString); | 700 &util::GetTimeFromString); |
| 701 converter->RegisterStringField(kTeamDriveId, &ChangeResource::team_drive_id_); |
| 702 converter->RegisterCustomValueField(kTeamDrive, &ChangeResource::team_drive_, |
| 703 &CreateTeamDriveResourceFromValue); |
677 } | 704 } |
678 | 705 |
679 // static | 706 // static |
680 std::unique_ptr<ChangeResource> ChangeResource::CreateFrom( | 707 std::unique_ptr<ChangeResource> ChangeResource::CreateFrom( |
681 const base::Value& value) { | 708 const base::Value& value) { |
682 std::unique_ptr<ChangeResource> resource(new ChangeResource()); | 709 std::unique_ptr<ChangeResource> resource(new ChangeResource()); |
683 if (!IsResourceKindExpected(value, kChangeKind) || !resource->Parse(value)) { | 710 if (!IsResourceKindExpected(value, kChangeKind) || !resource->Parse(value)) { |
684 LOG(ERROR) << "Unable to create: Invalid ChangeResource JSON!"; | 711 LOG(ERROR) << "Unable to create: Invalid ChangeResource JSON!"; |
685 return std::unique_ptr<ChangeResource>(); | 712 return std::unique_ptr<ChangeResource>(); |
686 } | 713 } |
687 return resource; | 714 return resource; |
688 } | 715 } |
689 | 716 |
690 bool ChangeResource::Parse(const base::Value& value) { | 717 bool ChangeResource::Parse(const base::Value& value) { |
691 base::JSONValueConverter<ChangeResource> converter; | 718 base::JSONValueConverter<ChangeResource> converter; |
692 if (!converter.Convert(value, this)) { | 719 if (!converter.Convert(value, this)) { |
693 LOG(ERROR) << "Unable to parse: Invalid ChangeResource"; | 720 LOG(ERROR) << "Unable to parse: Invalid ChangeResource"; |
694 return false; | 721 return false; |
695 } | 722 } |
696 return true; | 723 return true; |
697 } | 724 } |
698 | 725 |
| 726 // static |
| 727 bool ChangeResource::GetType(const base::StringPiece& type_name, |
| 728 ChangeResource::ChangeType* result) { |
| 729 for (size_t i = 0; i < arraysize(kChangeTypeMap); i++) { |
| 730 if (type_name == kChangeTypeMap[i].type_name) { |
| 731 *result = kChangeTypeMap[i].type; |
| 732 return true; |
| 733 } |
| 734 } |
| 735 DVLOG(1) << "Unknown change type" << type_name; |
| 736 return false; |
| 737 } |
| 738 |
699 //////////////////////////////////////////////////////////////////////////////// | 739 //////////////////////////////////////////////////////////////////////////////// |
700 // ChangeList implementation | 740 // ChangeList implementation |
701 | 741 |
702 ChangeList::ChangeList() : largest_change_id_(0) {} | 742 ChangeList::ChangeList() : largest_change_id_(0) {} |
703 | 743 |
704 ChangeList::~ChangeList() {} | 744 ChangeList::~ChangeList() {} |
705 | 745 |
706 // static | 746 // static |
707 void ChangeList::RegisterJSONConverter( | 747 void ChangeList::RegisterJSONConverter( |
708 base::JSONValueConverter<ChangeList>* converter) { | 748 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) { | 850 bool ImageMediaMetadata::Parse(const base::Value& value) { |
811 base::JSONValueConverter<ImageMediaMetadata> converter; | 851 base::JSONValueConverter<ImageMediaMetadata> converter; |
812 if (!converter.Convert(value, this)) { | 852 if (!converter.Convert(value, this)) { |
813 LOG(ERROR) << "Unable to parse: Invalid ImageMediaMetadata."; | 853 LOG(ERROR) << "Unable to parse: Invalid ImageMediaMetadata."; |
814 return false; | 854 return false; |
815 } | 855 } |
816 return true; | 856 return true; |
817 } | 857 } |
818 | 858 |
819 } // namespace google_apis | 859 } // namespace google_apis |
OLD | NEW |