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 "chrome/browser/chromeos/gdata/gdata_parser.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_parser.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 const char kSchemeAttr[] = "scheme"; | 127 const char kSchemeAttr[] = "scheme"; |
128 const char kSrcAttr[] = "src"; | 128 const char kSrcAttr[] = "src"; |
129 const char kTermAttr[] = "term"; | 129 const char kTermAttr[] = "term"; |
130 const char kTypeAttr[] = "type"; | 130 const char kTypeAttr[] = "type"; |
131 const char kValueAttr[] = "value"; | 131 const char kValueAttr[] = "value"; |
132 | 132 |
133 // Link Prefixes | 133 // Link Prefixes |
134 const char kOpenWithPrefix[] = "http://schemas.google.com/docs/2007#open-with-"; | 134 const char kOpenWithPrefix[] = "http://schemas.google.com/docs/2007#open-with-"; |
135 const size_t kOpenWithPrefixSize = arraysize(kOpenWithPrefix) - 1; | 135 const size_t kOpenWithPrefixSize = arraysize(kOpenWithPrefix) - 1; |
136 | 136 |
| 137 // Drive API JSON names. |
| 138 const char kQuotaBytesTotal[] = "quotaBytesTotal"; |
| 139 const char kQuotaBytesUsed[] = "quotaBytesUsed"; |
| 140 const char kLargestChangeId[] = "largestChangeId"; |
| 141 |
137 struct EntryKindMap { | 142 struct EntryKindMap { |
138 DocumentEntry::EntryKind kind; | 143 DocumentEntry::EntryKind kind; |
139 const char* entry; | 144 const char* entry; |
140 const char* extension; | 145 const char* extension; |
141 }; | 146 }; |
142 | 147 |
143 const EntryKindMap kEntryKindMap[] = { | 148 const EntryKindMap kEntryKindMap[] = { |
144 { DocumentEntry::ITEM, "item", NULL}, | 149 { DocumentEntry::ITEM, "item", NULL}, |
145 { DocumentEntry::DOCUMENT, "document", ".gdoc"}, | 150 { DocumentEntry::DOCUMENT, "document", ".gdoc"}, |
146 { DocumentEntry::SPREADSHEET, "spreadsheet", ".gsheet"}, | 151 { DocumentEntry::SPREADSHEET, "spreadsheet", ".gsheet"}, |
(...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 converter->RegisterRepeatedCustomValue( | 998 converter->RegisterRepeatedCustomValue( |
994 kInstalledAppSecondaryFileExtensionField, | 999 kInstalledAppSecondaryFileExtensionField, |
995 &InstalledApp::secondary_extensions_, | 1000 &InstalledApp::secondary_extensions_, |
996 &GetValueString); | 1001 &GetValueString); |
997 converter->RegisterRepeatedMessage(kLinkField, &InstalledApp::links_); | 1002 converter->RegisterRepeatedMessage(kLinkField, &InstalledApp::links_); |
998 } | 1003 } |
999 | 1004 |
1000 //////////////////////////////////////////////////////////////////////////////// | 1005 //////////////////////////////////////////////////////////////////////////////// |
1001 // AccountMetadataFeed implementation | 1006 // AccountMetadataFeed implementation |
1002 | 1007 |
| 1008 class AccountMetadataFeedDocumentList : public AccountMetadataFeed { |
| 1009 public: |
| 1010 AccountMetadataFeedDocumentList() : AccountMetadataFeed() {} |
| 1011 virtual ~AccountMetadataFeedDocumentList() {} |
| 1012 |
| 1013 // Registers the mapping between JSON field names and the members in this |
| 1014 // class. |
| 1015 static void RegisterJSONConverter( |
| 1016 base::JSONValueConverter<AccountMetadataFeedDocumentList>* converter); |
| 1017 |
| 1018 // Parses and initializes data members from content of |value|. |
| 1019 // Return false if parsing fails. |
| 1020 virtual bool Parse(const base::Value& value) OVERRIDE; |
| 1021 |
| 1022 private: |
| 1023 DISALLOW_COPY_AND_ASSIGN(AccountMetadataFeedDocumentList); |
| 1024 }; |
| 1025 |
| 1026 class AccountMetadataFeedDriveAPI : public AccountMetadataFeed { |
| 1027 public: |
| 1028 AccountMetadataFeedDriveAPI() : AccountMetadataFeed() {} |
| 1029 virtual ~AccountMetadataFeedDriveAPI() {} |
| 1030 |
| 1031 // Registers the mapping between JSON field names and the members in this |
| 1032 // class. |
| 1033 static void RegisterJSONConverter( |
| 1034 base::JSONValueConverter<AccountMetadataFeedDriveAPI>* converter); |
| 1035 |
| 1036 // Parses and initializes data members from content of |value|. |
| 1037 // Return false if parsing fails. |
| 1038 virtual bool Parse(const base::Value& value) OVERRIDE; |
| 1039 |
| 1040 private: |
| 1041 DISALLOW_COPY_AND_ASSIGN(AccountMetadataFeedDriveAPI); |
| 1042 }; |
| 1043 |
1003 AccountMetadataFeed::AccountMetadataFeed() | 1044 AccountMetadataFeed::AccountMetadataFeed() |
1004 : quota_bytes_total_(0), | 1045 : quota_bytes_total_(0), |
1005 quota_bytes_used_(0), | 1046 quota_bytes_used_(0), |
1006 largest_changestamp_(0) { | 1047 largest_changestamp_(0) { |
1007 } | 1048 } |
1008 | 1049 |
1009 AccountMetadataFeed::~AccountMetadataFeed() { | 1050 AccountMetadataFeed::~AccountMetadataFeed() { |
1010 } | 1051 } |
1011 | 1052 |
1012 // static | 1053 // static |
1013 void AccountMetadataFeed::RegisterJSONConverter( | |
1014 base::JSONValueConverter<AccountMetadataFeed>* converter) { | |
1015 converter->RegisterCustomField<int64>( | |
1016 kQuotaBytesTotalField, | |
1017 &AccountMetadataFeed::quota_bytes_total_, | |
1018 &base::StringToInt64); | |
1019 converter->RegisterCustomField<int64>( | |
1020 kQuotaBytesUsedField, | |
1021 &AccountMetadataFeed::quota_bytes_used_, | |
1022 &base::StringToInt64); | |
1023 converter->RegisterCustomField<int>( | |
1024 kLargestChangestampField, | |
1025 &AccountMetadataFeed::largest_changestamp_, | |
1026 &base::StringToInt); | |
1027 converter->RegisterRepeatedMessage(kInstalledAppField, | |
1028 &AccountMetadataFeed::installed_apps_); | |
1029 } | |
1030 | |
1031 // static | |
1032 scoped_ptr<AccountMetadataFeed> AccountMetadataFeed::CreateFrom( | 1054 scoped_ptr<AccountMetadataFeed> AccountMetadataFeed::CreateFrom( |
1033 const base::Value& value) { | 1055 const base::Value& value, bool use_drive_api) { |
1034 scoped_ptr<AccountMetadataFeed> feed(new AccountMetadataFeed()); | 1056 if (use_drive_api) { |
| 1057 scoped_ptr<AccountMetadataFeedDriveAPI> feed( |
| 1058 new AccountMetadataFeedDriveAPI()); |
| 1059 if (!feed->Parse(value)) { |
| 1060 LOG(ERROR) << "Unable to create: Invalid account metadata feed!"; |
| 1061 return scoped_ptr<AccountMetadataFeed>(NULL); |
| 1062 } |
| 1063 return feed.PassAs<AccountMetadataFeed>(); |
| 1064 } |
| 1065 |
| 1066 scoped_ptr<AccountMetadataFeedDocumentList> feed( |
| 1067 new AccountMetadataFeedDocumentList()); |
1035 const base::DictionaryValue* dictionary = NULL; | 1068 const base::DictionaryValue* dictionary = NULL; |
1036 base::Value* entry = NULL; | 1069 base::Value* entry = NULL; |
1037 if (!value.GetAsDictionary(&dictionary) || | 1070 if (!value.GetAsDictionary(&dictionary) || |
1038 !dictionary->Get(kEntryField, &entry) || | 1071 !dictionary->Get(kEntryField, &entry) || |
1039 !feed->Parse(*entry)) { | 1072 !feed->Parse(*entry)) { |
1040 LOG(ERROR) << "Unable to create: Invalid account metadata feed!"; | 1073 LOG(ERROR) << "Unable to create: Invalid account metadata feed!"; |
1041 return scoped_ptr<AccountMetadataFeed>(NULL); | 1074 return scoped_ptr<AccountMetadataFeed>(NULL); |
1042 } | 1075 } |
1043 | 1076 return feed.PassAs<AccountMetadataFeed>(); |
1044 return feed.Pass(); | |
1045 } | 1077 } |
1046 | 1078 |
1047 bool AccountMetadataFeed::Parse(const base::Value& value) { | 1079 // static |
1048 base::JSONValueConverter<AccountMetadataFeed> converter; | 1080 void AccountMetadataFeedDocumentList::RegisterJSONConverter( |
| 1081 base::JSONValueConverter<AccountMetadataFeedDocumentList>* converter) { |
| 1082 converter->RegisterCustomField<int64>( |
| 1083 kQuotaBytesTotalField, |
| 1084 &AccountMetadataFeedDocumentList::quota_bytes_total_, |
| 1085 &base::StringToInt64); |
| 1086 converter->RegisterCustomField<int64>( |
| 1087 kQuotaBytesUsedField, |
| 1088 &AccountMetadataFeedDocumentList::quota_bytes_used_, |
| 1089 &base::StringToInt64); |
| 1090 converter->RegisterCustomField<int>( |
| 1091 kLargestChangestampField, |
| 1092 &AccountMetadataFeedDocumentList::largest_changestamp_, |
| 1093 &base::StringToInt); |
| 1094 converter->RegisterRepeatedMessage<InstalledApp>( |
| 1095 kInstalledAppField, |
| 1096 &AccountMetadataFeedDocumentList::installed_apps_); |
| 1097 } |
| 1098 |
| 1099 bool AccountMetadataFeedDocumentList::Parse(const base::Value& value) { |
| 1100 base::JSONValueConverter<AccountMetadataFeedDocumentList> converter; |
1049 if (!converter.Convert(value, this)) { | 1101 if (!converter.Convert(value, this)) { |
1050 LOG(ERROR) << "Unable to parse: Invalid account metadata feed!"; | 1102 LOG(ERROR) << "Unable to parse: Invalid account metadata feed!"; |
1051 return false; | 1103 return false; |
| 1104 } |
| 1105 return true; |
| 1106 } |
| 1107 |
| 1108 // static |
| 1109 void AccountMetadataFeedDriveAPI::RegisterJSONConverter( |
| 1110 base::JSONValueConverter<AccountMetadataFeedDriveAPI>* converter) { |
| 1111 converter->RegisterCustomField<int64>( |
| 1112 kQuotaBytesTotal, |
| 1113 &AccountMetadataFeedDriveAPI::quota_bytes_total_, |
| 1114 &base::StringToInt64); |
| 1115 converter->RegisterCustomField<int64>( |
| 1116 kQuotaBytesUsed, |
| 1117 &AccountMetadataFeedDriveAPI::quota_bytes_used_, |
| 1118 &base::StringToInt64); |
| 1119 converter->RegisterCustomField<int>( |
| 1120 kLargestChangeId, |
| 1121 &AccountMetadataFeedDriveAPI::largest_changestamp_, |
| 1122 &base::StringToInt); |
| 1123 // TODO(kochi): To get installed apps, Drive V2 API needs another API call. |
| 1124 } |
| 1125 |
| 1126 bool AccountMetadataFeedDriveAPI::Parse(const base::Value& value) { |
| 1127 base::JSONValueConverter<AccountMetadataFeedDriveAPI> converter; |
| 1128 if (!converter.Convert(value, this)) { |
| 1129 LOG(ERROR) << "Unable to parse: Invalid account metadata feed!"; |
| 1130 return false; |
1052 } | 1131 } |
1053 return true; | 1132 return true; |
1054 } | 1133 } |
1055 | 1134 |
1056 } // namespace gdata | 1135 } // namespace gdata |
OLD | NEW |