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

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: rebase. Created 8 years, 4 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
72 // Maps category name to enum IconCategory.
73 struct AppIconCategoryMap {
74 gdata::DriveAppIcon::IconCategory category;
75 const char* category_name;
76 };
77
78 const AppIconCategoryMap kAppIconCategoryMap[] = {
79 { gdata::DriveAppIcon::DOCUMENT, "document" },
80 { gdata::DriveAppIcon::APPLICATION, "application" },
81 { gdata::DriveAppIcon::SHARED_DOCUMENT, "documentShared" },
82 };
83
84 // Checks if the JSON is expected kind. In Drive API, JSON data structure has
85 // |kind| property which denotes the type of the structure (e.g. "drive#file").
86 bool IsResourceKindExpected(const base::Value& value,
87 const std::string& expected_kind) {
88 const base::DictionaryValue* as_dict = NULL;
89 std::string kind;
90 return value.GetAsDictionary(&as_dict) &&
91 as_dict->HasKey(kKind) &&
92 as_dict->GetString(kKind, &kind) &&
93 kind == expected_kind;
94 }
95
96 } // namespace
97
98 // TODO(kochi): Rename to namespace drive. http://crbug.com/136371
99 namespace gdata {
100
101 ////////////////////////////////////////////////////////////////////////////////
102 // AboutResource implementation
103
104 AboutResource::AboutResource()
105 : quota_bytes_total_(0),
106 quota_bytes_used_(0),
107 largest_change_id_(0) {}
108
109 AboutResource::~AboutResource() {}
110
111 // static
112 scoped_ptr<AboutResource> AboutResource::CreateFrom(const base::Value& value) {
113 scoped_ptr<AboutResource> resource(new AboutResource());
114 if (!IsResourceKindExpected(value, kAboutKind) || !resource->Parse(value)) {
115 LOG(ERROR) << "Unable to create: Invalid About resource JSON!";
116 return scoped_ptr<AboutResource>(NULL);
117 }
118 return resource.Pass();
119 }
120
121 // static
122 void AboutResource::RegisterJSONConverter(
123 base::JSONValueConverter<AboutResource>* converter) {
124 converter->RegisterStringField(kRootFolderId,
125 &AboutResource::root_folder_id_);
126 converter->RegisterCustomField<int64>(kQuotaBytesTotal,
127 &AboutResource::quota_bytes_total_,
128 &base::StringToInt64);
129 converter->RegisterCustomField<int64>(kQuotaBytesUsed,
130 &AboutResource::quota_bytes_used_,
131 &base::StringToInt64);
132 converter->RegisterCustomField<int64>(kLargestChangeId,
133 &AboutResource::largest_change_id_,
134 &base::StringToInt64);
135 }
136
137 bool AboutResource::Parse(const base::Value& value) {
138 base::JSONValueConverter<AboutResource> converter;
139 if (!converter.Convert(value, this)) {
140 LOG(ERROR) << "Unable to parse: Invalid About resource JSON!";
141 return false;
142 }
143 return true;
144 }
145
146 ////////////////////////////////////////////////////////////////////////////////
147 // DriveAppIcon implementation
148
149 DriveAppIcon::DriveAppIcon() {}
150
151 DriveAppIcon::~DriveAppIcon() {}
152
153 // static
154 void DriveAppIcon::RegisterJSONConverter(
155 base::JSONValueConverter<DriveAppIcon>* converter) {
156 converter->RegisterCustomField<IconCategory>(
157 kCategory,
158 &DriveAppIcon::category_,
159 &DriveAppIcon::GetIconCategory);
160 converter->RegisterIntField(kSize, &DriveAppIcon::icon_side_length_);
161 converter->RegisterCustomField<GURL>(kIconUrl,
162 &DriveAppIcon::icon_url_,
163 GetGURLFromString);
164 }
165
166 // static
167 scoped_ptr<DriveAppIcon> DriveAppIcon::CreateFrom(const base::Value& value) {
168 scoped_ptr<DriveAppIcon> resource(new DriveAppIcon());
169 if (!resource->Parse(value)) {
170 LOG(ERROR) << "Unable to create: Invalid DriveAppIcon JSON!";
171 return scoped_ptr<DriveAppIcon>(NULL);
172 }
173 return resource.Pass();
174 }
175
176 bool DriveAppIcon::Parse(const base::Value& value) {
177 base::JSONValueConverter<DriveAppIcon> converter;
178 if (!converter.Convert(value, this)) {
179 LOG(ERROR) << "Unable to parse: Invalid DriveAppIcon";
180 return false;
181 }
182 return true;
183 }
184
185 // static
186 bool DriveAppIcon::GetIconCategory(const base::StringPiece& category,
187 DriveAppIcon::IconCategory* result) {
188 for (size_t i = 0; i < arraysize(kAppIconCategoryMap); i++) {
189 if (category == kAppIconCategoryMap[i].category_name) {
190 *result = kAppIconCategoryMap[i].category;
191 return true;
192 }
193 }
194 DVLOG(1) << "Unknown icon category " << category;
195 return false;
196 }
197
198 ////////////////////////////////////////////////////////////////////////////////
199 // AppResource implementation
200
201 AppResource::AppResource() {}
202
203 AppResource::~AppResource() {}
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 (!IsResourceKindExpected(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 (!IsResourceKindExpected(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
« no previous file with comments | « chrome/browser/chromeos/gdata/drive_api_parser.h ('k') | chrome/browser/chromeos/gdata/drive_api_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698