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

Side by Side Diff: chrome/browser/chromeos/gdata/drive_api_parser.cc

Issue 10810070: Add Drive API parser for About/Apps json (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/gdata/drive_api_parser.h"
6
7 #include <algorithm>
8
9 #include "base/basictypes.h"
10 #include "base/file_path.h"
11 #include "base/json/json_value_converter.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/string_number_conversions.h"
14 #include "base/string_piece.h"
15 #include "base/string_util.h"
16 #include "base/values.h"
17 #include "chrome/browser/chromeos/gdata/gdata_util.h"
18
19 using base::Value;
20 using base::DictionaryValue;
21 using base::ListValue;
22
23 namespace {
24
25 // Converts |url_string| to |result|. Always returns true to be used
26 // for JSONValueConverter::RegisterCustomField method.
27 // TODO(mukai): make it return false in case of invalid |url_string|.
28 bool GetGURLFromString(const base::StringPiece& url_string, GURL* result) {
29 *result = GURL(url_string.as_string());
30 return true;
31 }
32
33 // Drive v2 API JSON names.
34
35 // Common
36 const char kKind[] = "kind";
37
38 // About Resource:
39 const char kAboutKind[] = "drive#about";
40 const char kRootFolderId[] = "rootFolderId";
41 const char kQuotaBytesTotal[] = "quotaBytesTotal";
42 const char kQuotaBytesUsed[] = "quotaBytesUsed";
43 const char kLargestChangeId[] = "largestChangeId";
44
45 // App Icon
46 const char kCategory[] = "category";
47 const char kSize[] = "size";
48 const char kIconUrl[] = "iconUrl";
49
50 // Apps Resource:
51 const char kAppKind[] = "drive#app";
52 const char kId[] = "id";
53 const char kETag[] = "etag";
54 const char kName[] = "name";
55 const char kObjectType[] = "objectType";
56 const char kSupportsCreate[] = "supportsCreate";
57 const char kSupportsImport[] = "supportsImport";
58 const char kInstalled[] = "installed";
59 const char kAuthorized[] = "authorized";
60 const char kProductUrl[] = "productUrl";
61 const char kPrimaryMimeTypes[] = "primaryMimeTypes";
62 const char kSecondaryMimeTypes[] = "secondaryMimeTypes";
63 const char kPrimaryFileExtensions[] = "primaryFileExtensions";
64 const char kSecondaryFileExtensions[] = "secondaryFileExtensions";
65 const char kIcons[] = "icons";
66
67 // Apps List:
68 const char kAppListKind[] = "drive#appList";
69 const char kItems[] = "items";
70
71 struct AppIconCategoryMap {
72 gdata::DriveAppIcon::IconCategory category;
73 const char* category_name;
74 };
75
76 const AppIconCategoryMap kAppIconCategoryMap[] = {
77 { gdata::DriveAppIcon::DOCUMENT, "document" },
78 { gdata::DriveAppIcon::APPLICATION, "application" },
79 { gdata::DriveAppIcon::SHARED_DOCUMENT, "documentShared" },
80 };
81
82 bool CheckResourceKind(const base::Value& value,
83 const std::string& expected_kind) {
84 const base::DictionaryValue* as_dict = NULL;
85 std::string kind;
86 return value.GetAsDictionary(&as_dict) &&
87 as_dict->HasKey(kKind) &&
88 as_dict->GetString(kKind, &kind) &&
89 kind == expected_kind;
90 }
91
92 } // namespace
93
94 // TODO(kochi): Rename to namespace drive. http://crbug.com/136371
95 namespace gdata {
96
97 ////////////////////////////////////////////////////////////////////////////////
98 // AboutResource implementation
99
100 AboutResource::AboutResource()
101 : quota_bytes_total_(0),
102 quota_bytes_used_(0),
103 largest_change_id_(0) {}
104
105 AboutResource::~AboutResource() {}
106
107 // static
108 scoped_ptr<AboutResource> AboutResource::CreateFrom(const base::Value& value) {
109 scoped_ptr<AboutResource> resource(new AboutResource());
110 if (!CheckResourceKind(value, kAboutKind) || !resource->Parse(value)) {
111 LOG(ERROR) << "Unable to create: Invalid About resource JSON!";
112 return scoped_ptr<AboutResource>(NULL);
113 }
114 return resource.Pass();
115 }
116
117 // static
118 void AboutResource::RegisterJSONConverter(
119 base::JSONValueConverter<AboutResource>* converter) {
120 converter->RegisterStringField(kRootFolderId,
121 &AboutResource::root_folder_id_);
122 converter->RegisterCustomField<int64>(kQuotaBytesTotal,
123 &AboutResource::quota_bytes_total_,
124 &base::StringToInt64);
125 converter->RegisterCustomField<int64>(kQuotaBytesUsed,
126 &AboutResource::quota_bytes_used_,
127 &base::StringToInt64);
128 converter->RegisterCustomField<int64>(kLargestChangeId,
129 &AboutResource::largest_change_id_,
130 &base::StringToInt64);
131 }
132
133 bool AboutResource::Parse(const base::Value& value) {
134 base::JSONValueConverter<AboutResource> converter;
135 if (!converter.Convert(value, this)) {
136 LOG(ERROR) << "Unable to parse: Invalid About resource JSON!";
137 return false;
138 }
139 return true;
140 }
141
142 ////////////////////////////////////////////////////////////////////////////////
143 // DriveAppIcon implementation
144
145 DriveAppIcon::DriveAppIcon() {
146 }
147
148 DriveAppIcon::~DriveAppIcon() {
149 }
150
151 // static
152 void DriveAppIcon::RegisterJSONConverter(
153 base::JSONValueConverter<DriveAppIcon>* converter) {
154 converter->RegisterCustomField<IconCategory>(
155 kCategory,
156 &DriveAppIcon::category_,
157 &DriveAppIcon::GetIconCategory);
158 converter->RegisterIntField(kSize, &DriveAppIcon::size_);
159 converter->RegisterCustomField<GURL>(kIconUrl,
160 &DriveAppIcon::icon_url_,
161 GetGURLFromString);
162 }
163
164 // static
165 scoped_ptr<DriveAppIcon> DriveAppIcon::CreateFrom(const base::Value& value) {
166 scoped_ptr<DriveAppIcon> resource(new DriveAppIcon());
167 if (!resource->Parse(value)) {
168 LOG(ERROR) << "Unable to create: Invalid DriveAppIcon JSON!";
169 return scoped_ptr<DriveAppIcon>(NULL);
170 }
171 return resource.Pass();
172 }
173
174 bool DriveAppIcon::Parse(const base::Value& value) {
175 base::JSONValueConverter<DriveAppIcon> converter;
176 if (!converter.Convert(value, this)) {
177 LOG(ERROR) << "Unable to parse: Invalid DriveAppIcon";
178 return false;
179 }
180 return true;
181 }
182
183 // static
184 bool DriveAppIcon::GetIconCategory(const base::StringPiece& category,
185 DriveAppIcon::IconCategory* result) {
186 for (size_t i = 0; i < arraysize(kAppIconCategoryMap); i++) {
187 if (category == kAppIconCategoryMap[i].category_name) {
188 *result = kAppIconCategoryMap[i].category;
189 return true;
190 }
191 }
192 DVLOG(1) << "Unknown icon category " << category;
193 return false;
194 }
195
196 ////////////////////////////////////////////////////////////////////////////////
197 // AppResource implementation
198
199 AppResource::AppResource() {
200 }
201
202 AppResource::~AppResource() {
203 }
204
205 // static
206 void AppResource::RegisterJSONConverter(
207 base::JSONValueConverter<AppResource>* converter) {
208 converter->RegisterStringField(kId, &AppResource::id_);
209 converter->RegisterStringField(kName, &AppResource::name_);
210 converter->RegisterStringField(kObjectType, &AppResource::object_type_);
211 converter->RegisterBoolField(kSupportsCreate, &AppResource::supports_create_);
212 converter->RegisterBoolField(kSupportsImport, &AppResource::supports_import_);
213 converter->RegisterBoolField(kInstalled, &AppResource::installed_);
214 converter->RegisterBoolField(kAuthorized, &AppResource::authorized_);
215 converter->RegisterCustomField<GURL>(kProductUrl,
216 &AppResource::product_url_,
217 GetGURLFromString);
218 converter->RegisterRepeatedString(kPrimaryMimeTypes,
219 &AppResource::primary_mimetypes_);
220 converter->RegisterRepeatedString(kSecondaryMimeTypes,
221 &AppResource::secondary_mimetypes_);
222 converter->RegisterRepeatedString(kPrimaryFileExtensions,
223 &AppResource::primary_file_extensions_);
224 converter->RegisterRepeatedString(kSecondaryFileExtensions,
225 &AppResource::secondary_file_extensions_);
226 converter->RegisterRepeatedMessage(kIcons, &AppResource::icons_);
227 }
228
229 // static
230 scoped_ptr<AppResource> AppResource::CreateFrom(const base::Value& value) {
231 scoped_ptr<AppResource> resource(new AppResource());
232 if (!CheckResourceKind(value, kAppKind) || !resource->Parse(value)) {
233 LOG(ERROR) << "Unable to create: Invalid AppResource JSON!";
234 return scoped_ptr<AppResource>(NULL);
235 }
236 return resource.Pass();
237 }
238
239 bool AppResource::Parse(const base::Value& value) {
240 base::JSONValueConverter<AppResource> converter;
241 if (!converter.Convert(value, this)) {
242 LOG(ERROR) << "Unable to parse: Invalid AppResource";
243 return false;
244 }
245 return true;
246 }
247
248 ////////////////////////////////////////////////////////////////////////////////
249 // AppList implementation
250
251 AppList::AppList() {}
252
253 AppList::~AppList() {}
254
255 // static
256 void AppList::RegisterJSONConverter(
257 base::JSONValueConverter<AppList>* converter) {
258 converter->RegisterStringField(kETag, &AppList::etag_);
259 converter->RegisterRepeatedMessage<AppResource>(kItems,
260 &AppList::items_);
261 }
262
263 // static
264 scoped_ptr<AppList> AppList::CreateFrom(const base::Value& value) {
265 scoped_ptr<AppList> resource(new AppList());
266 if (!CheckResourceKind(value, kAppListKind) || !resource->Parse(value)) {
267 LOG(ERROR) << "Unable to create: Invalid AppList JSON!";
268 return scoped_ptr<AppList>(NULL);
269 }
270 return resource.Pass();
271 }
272
273 bool AppList::Parse(const base::Value& value) {
274 base::JSONValueConverter<AppList> converter;
275 if (!converter.Convert(value, this)) {
276 LOG(ERROR) << "Unable to parse: Invalid AppList";
277 return false;
278 }
279 return true;
280 }
281
282 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698