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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_parser.h

Issue 10692152: Rename gdata_parser to gdata_wapi_parser (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 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_PARSER_H_
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_PARSER_H_
7 #pragma once
8
9 #include <string>
10 #include <vector>
11
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/string_piece.h"
16 #include "base/time.h"
17 #include "googleurl/src/gurl.h"
18
19 class FilePath;
20 class Profile;
21 class XmlReader;
22
23 namespace base {
24 class Value;
25 class DictionaryValue;
26 template <class StructType>
27 class JSONValueConverter;
28
29 namespace internal {
30 template <class NestedType>
31 class RepeatedMessageConverter;
32 } // namespace internal
33
34 } // namespace base
35
36 // Defines data elements of Google Documents API as described in
37 // http://code.google.com/apis/documents/.
38 namespace gdata {
39
40 // Defines link (URL) of an entity (document, file, feed...). Each entity could
41 // have more than one link representing it.
42 class Link {
43 public:
44 enum LinkType {
45 UNKNOWN,
46 SELF,
47 NEXT,
48 PARENT,
49 ALTERNATE,
50 EDIT,
51 EDIT_MEDIA,
52 ALT_EDIT_MEDIA,
53 ALT_POST,
54 FEED,
55 POST,
56 BATCH,
57 RESUMABLE_EDIT_MEDIA,
58 RESUMABLE_CREATE_MEDIA,
59 TABLES_FEED,
60 WORKSHEET_FEED,
61 THUMBNAIL,
62 EMBED,
63 PRODUCT,
64 ICON,
65 OPEN_WITH,
66 };
67 Link();
68 ~Link();
69
70 // Registers the mapping between JSON field names and the members in
71 // this class.
72 static void RegisterJSONConverter(base::JSONValueConverter<Link>* converter);
73
74 // Creates document entry from parsed XML.
75 static Link* CreateFromXml(XmlReader* xml_reader);
76
77 // Type of the link.
78 LinkType type() const { return type_; }
79
80 // URL of the link.
81 const GURL& href() const { return href_; }
82
83 // Title of the link.
84 const string16& title() const { return title_; }
85
86 // For OPEN_WITH links, this contains the application ID. For all other link
87 // types, it is the empty string.
88 const std::string& app_id() const { return app_id_; }
89
90 // Link MIME type.
91 const std::string& mime_type() const { return mime_type_; }
92
93 private:
94 friend class DocumentEntry;
95 // Converts value of link.rel into LinkType. Outputs to |type| and returns
96 // true when |rel| has a valid value. Otherwise does nothing and returns
97 // false.
98 static bool GetLinkType(const base::StringPiece& rel, LinkType* type);
99
100 // Converts value of link.rel to application ID, if there is one embedded in
101 // the link.rel field. Outputs to |app_id| and returns true when |rel| has a
102 // valid value. Otherwise does nothing and returns false.
103 static bool GetAppID(const base::StringPiece& rel, std::string* app_id);
104
105
106 LinkType type_;
107 GURL href_;
108 string16 title_;
109 std::string app_id_;
110 std::string mime_type_;
111
112 DISALLOW_COPY_AND_ASSIGN(Link);
113 };
114
115 // Feed links define links (URLs) to special list of entries (i.e. list of
116 // previous document revisions).
117 class FeedLink {
118 public:
119 enum FeedLinkType {
120 UNKNOWN,
121 ACL,
122 REVISIONS,
123 };
124 FeedLink();
125
126 // Registers the mapping between JSON field names and the members in
127 // this class.
128 static void RegisterJSONConverter(
129 base::JSONValueConverter<FeedLink>* converter);
130
131 static FeedLink* CreateFromXml(XmlReader* xml_reader);
132
133 // MIME type of the feed.
134 FeedLinkType type() const { return type_; }
135
136 // URL of the feed.
137 const GURL& href() const { return href_; }
138
139 private:
140 friend class DocumentEntry;
141 // Converts value of gd$feedLink.rel into FeedLinkType enum.
142 // Outputs to |result| and returns true when |rel| has a valid
143 // value. Otherwise does nothing and returns false.
144 static bool GetFeedLinkType(
145 const base::StringPiece& rel, FeedLinkType* result);
146
147 FeedLinkType type_;
148 GURL href_;
149
150 DISALLOW_COPY_AND_ASSIGN(FeedLink);
151 };
152
153 // Author represents an author of an entity.
154 class Author {
155 public:
156 Author();
157
158 // Registers the mapping between JSON field names and the members in
159 // this class.
160 static void RegisterJSONConverter(
161 base::JSONValueConverter<Author>* converter);
162
163 static Author* CreateFromXml(XmlReader* xml_reader);
164
165 // Getters.
166 const string16& name() const { return name_; }
167 const std::string& email() const { return email_; }
168
169 private:
170 friend class DocumentEntry;
171
172 string16 name_;
173 std::string email_;
174
175 DISALLOW_COPY_AND_ASSIGN(Author);
176 };
177
178 // Entry category.
179 class Category {
180 public:
181 enum CategoryType {
182 UNKNOWN,
183 ITEM,
184 KIND,
185 LABEL,
186 };
187
188 Category();
189
190 // Registers the mapping between JSON field names and the members in
191 // this class.
192 static void RegisterJSONConverter(
193 base::JSONValueConverter<Category>* converter);
194
195 static Category* CreateFromXml(XmlReader* xml_reader);
196
197 // Category label.
198 const string16& label() const { return label_; }
199
200 // Category type.
201 CategoryType type() const { return type_; }
202
203 // Category term.
204 const std::string& term() const { return term_; }
205
206 private:
207 friend class DocumentEntry;
208 // Converts category scheme into CategoryType enum. For example,
209 // http://schemas.google.com/g/2005#kind => Category::KIND
210 // Returns false and does not change |result| when |scheme| has an
211 // unrecognizable value.
212 static bool GetCategoryTypeFromScheme(
213 const base::StringPiece& scheme, CategoryType* result);
214
215 string16 label_;
216 CategoryType type_;
217 std::string term_;
218
219 DISALLOW_COPY_AND_ASSIGN(Category);
220 };
221
222 // Content details of a document: mime-type, url, and so on.
223 class Content {
224 public:
225 Content();
226
227 // Registers the mapping between JSON field names and the members in
228 // this class.
229 static void RegisterJSONConverter(
230 base::JSONValueConverter<Content>* converter);
231
232 static Content* CreateFromXml(XmlReader* xml_reader);
233
234 const GURL& url() const { return url_; }
235 const std::string& mime_type() const { return mime_type_; }
236
237 private:
238 friend class DocumentEntry;
239
240 GURL url_;
241 std::string mime_type_;
242 };
243
244 // This stores a representation of an application icon as registered with the
245 // installed applications section of the account metadata feed. There can be
246 // multiple icons registered for each application, differing in size, category
247 // and MIME type.
248 class AppIcon {
249 public:
250 enum IconCategory {
251 UNKNOWN, // Uninitialized state
252 DOCUMENT, // Document icon for various MIME types
253 APPLICATION, // Application icon for various MIME types
254 SHARED_DOCUMENT, // Icon for documents that are shared from other users.
255 };
256
257 AppIcon();
258 ~AppIcon();
259
260 // Registers the mapping between JSON field names and the members in
261 // this class.
262 static void RegisterJSONConverter(
263 base::JSONValueConverter<AppIcon>* converter);
264
265 // Category of the icon.
266 IconCategory category() const { return category_; }
267
268 // Size in pixels of one side of the icon (icons are always square).
269 const int icon_side_length() const { return icon_side_length_; }
270
271 // Get a list of links available for this AppIcon.
272 const ScopedVector<Link>& links() const { return links_; }
273
274 // Get the icon URL from the internal list of links. Returns the first
275 // icon URL found in the list.
276 GURL GetIconURL() const;
277
278 private:
279 // Extracts the icon category from the given string. Returns false and does
280 // not change |result| when |scheme| has an unrecognizable value.
281 static bool GetIconCategory(const base::StringPiece& category,
282 IconCategory* result);
283
284 IconCategory category_;
285 int icon_side_length_;
286 ScopedVector<Link> links_;
287
288 DISALLOW_COPY_AND_ASSIGN(AppIcon);
289 };
290
291 // Base class for feed entries.
292 class FeedEntry {
293 public:
294 FeedEntry();
295 virtual ~FeedEntry();
296
297 // Returns a link of a given |type| for this entry. If not found, it returns
298 // NULL.
299 const Link* GetLinkByType(Link::LinkType type) const;
300
301 // Entry update time.
302 base::Time updated_time() const { return updated_time_; }
303
304 // Entry ETag.
305 const std::string& etag() const { return etag_; }
306
307 // List of entry authors.
308 const ScopedVector<Author>& authors() const { return authors_; }
309
310 // List of entry links.
311 const ScopedVector<Link>& links() const { return links_; }
312
313 // List of entry categories.
314 const ScopedVector<Category>& categories() const { return categories_; }
315
316 // Returns true when time string (in format yyyy-mm-ddThh:mm:ss.dddZ), is
317 // successfully parsed and output as |time|.
318 static bool GetTimeFromString(
319 const base::StringPiece& raw_value, base::Time* time);
320
321 // Registers the mapping between JSON field names and the members in
322 // this class.
323 static void RegisterJSONConverter(
324 base::JSONValueConverter<FeedEntry>* converter);
325
326 protected:
327 std::string etag_;
328 ScopedVector<Author> authors_;
329 ScopedVector<Link> links_;
330 ScopedVector<Category> categories_;
331 base::Time updated_time_;
332
333 DISALLOW_COPY_AND_ASSIGN(FeedEntry);
334 };
335
336 // Document feed entry.
337 class DocumentEntry : public FeedEntry {
338 public:
339 // kEntryKindMap should be also updated if you modify EntryKind.
340 enum EntryKind {
341 UNKNOWN = 0x000000,
342 // Special entries.
343 ITEM = 0x001001,
344 SITE = 0x001002,
345 // Hosted Google document.
346 DOCUMENT = 0x002101,
347 SPREADSHEET = 0x002102,
348 PRESENTATION = 0x002103,
349 DRAWING = 0x002104,
350 TABLE = 0x002105,
351 // Hosted external application document.
352 EXTERNAL_APP = 0x002201,
353 // Folders, collections.
354 FOLDER = 0x004001,
355 // Regular files.
356 FILE = 0x008001,
357 PDF = 0x008002,
358 };
359 virtual ~DocumentEntry();
360
361 // Extracts "entry" dictionary from the JSON value, and parse the contents,
362 // using CreateFrom(). Returns NULL on failure. The input JSON data, coming
363 // from the gdata server, looks like:
364 //
365 // {
366 // "encoding": "UTF-8",
367 // "entry": { ... }, // This function will extract this and parse.
368 // "version": "1.0"
369 // }
370 static DocumentEntry* ExtractAndParse(const base::Value& value);
371
372 // Creates document entry from parsed JSON Value. You should call
373 // this instead of instantiating JSONValueConverter by yourself
374 // because this method does some post-process for some fields. See
375 // FillRemainingFields comment and implementation for the details.
376 static DocumentEntry* CreateFrom(const base::Value* value);
377
378 // Creates document entry from parsed XML.
379 static DocumentEntry* CreateFromXml(XmlReader* xml_reader);
380
381 // Returns name of entry node.
382 static std::string GetEntryNodeName();
383
384 // Registers the mapping between JSON field names and the members in
385 // this class.
386 static void RegisterJSONConverter(
387 base::JSONValueConverter<DocumentEntry>* converter);
388
389 // Helper function for parsing bool fields based on presence of
390 // their value nodes.
391 static bool HasFieldPresent(const base::Value* value, bool* result);
392
393 // Returns true if |file| has one of the hosted document extensions.
394 static bool HasHostedDocumentExtension(const FilePath& file);
395
396 // Returns a list of all entry kinds.
397 static std::vector<int> GetAllEntryKinds();
398
399 // Document entry resource id.
400 const std::string& resource_id() const { return resource_id_; }
401
402 // Document entry id.
403 const std::string& id() const { return id_; }
404
405 // Document entry kind.
406 EntryKind kind() const { return kind_; }
407
408 // Document entry title.
409 const string16& title() const { return title_; }
410
411 // Document entry published time.
412 base::Time published_time() const { return published_time_; }
413
414 // List of document feed labels.
415 const std::vector<string16>& labels() const { return labels_; }
416
417 // Document entry content URL.
418 const GURL& content_url() const { return content_.url(); }
419
420 // Document entry MIME type.
421 const std::string& content_mime_type() const { return content_.mime_type(); }
422
423 // List of document feed links.
424 const ScopedVector<FeedLink>& feed_links() const { return feed_links_; }
425
426 // Document feed file name (exists only for kinds FILE and PDF).
427 const string16& filename() const { return filename_; }
428
429 // Document feed suggested file name (exists only for kinds FILE and PDF).
430 const string16& suggested_filename() const { return suggested_filename_; }
431
432 // Document feed file content MD5 (exists only for kinds FILE and PDF).
433 const std::string& file_md5() const { return file_md5_; }
434
435 // Document feed file size (exists only for kinds FILE and PDF).
436 int64 file_size() const { return file_size_; }
437
438 // True if the file or directory is deleted (applicable to change feeds only).
439 bool deleted() const { return deleted_ || removed_; }
440
441 // Text version of document entry kind. Returns an empty string for
442 // unknown entry kind.
443 std::string GetEntryKindText() const;
444
445 // Returns preferred file extension for hosted documents. If entry is not
446 // a hosted document, this call returns an empty string.
447 std::string GetHostedDocumentExtension() const;
448
449 // True if document entry is remotely hosted.
450 bool is_hosted_document() const { return (kind_ & 0x002000) == 0x002000; }
451 // True if document entry hosted by Google Documents.
452 bool is_google_document() const { return (kind_ & 0x002100) == 0x002100; }
453 // True if document entry is hosted by an external application.
454 bool is_external_document() const { return (kind_ & 0x002200) == 0x002200; }
455 // True if document entry is a folder (collection).
456 bool is_folder() const { return (kind_ & 0x004000) != 0; }
457 // True if document entry is regular file.
458 bool is_file() const { return (kind_ & 0x008000) != 0; }
459 // True if document entry can't be mapped to the file system.
460 bool is_special() const {
461 return !is_file() && !is_folder() && !is_hosted_document();
462 }
463
464 private:
465 friend class base::internal::RepeatedMessageConverter<DocumentEntry>;
466 friend class DocumentFeed;
467 friend class ResumeUploadOperation;
468
469 DocumentEntry();
470
471 // Fills the remaining fields where JSONValueConverter cannot catch.
472 void FillRemainingFields();
473
474 // Converts categories.term into EntryKind enum.
475 static EntryKind GetEntryKindFromTerm(const std::string& term);
476 // Converts |kind| into its text identifier equivalent.
477 static const char* GetEntryKindDescription(EntryKind kind);
478
479 std::string resource_id_;
480 std::string id_;
481 EntryKind kind_;
482 string16 title_;
483 base::Time published_time_;
484 std::vector<string16> labels_;
485 Content content_;
486 ScopedVector<FeedLink> feed_links_;
487 // Optional fields for files only.
488 string16 filename_;
489 string16 suggested_filename_;
490 std::string file_md5_;
491 int64 file_size_;
492 bool deleted_;
493 bool removed_;
494
495 DISALLOW_COPY_AND_ASSIGN(DocumentEntry);
496 };
497
498 // Document feed represents a list of entries. The feed is paginated and
499 // the rest of the feed can be fetched by retrieving the remaining parts of the
500 // feed from URLs provided by GetNextFeedURL() method.
501 class DocumentFeed : public FeedEntry {
502 public:
503 virtual ~DocumentFeed();
504
505 // Extracts "feed" dictionary from the JSON value, and parse the contents,
506 // using CreateFrom(). Returns NULL on failure. The input JSON data, coming
507 // from the gdata server, looks like:
508 //
509 // {
510 // "encoding": "UTF-8",
511 // "feed": { ... }, // This function will extract this and parse.
512 // "version": "1.0"
513 // }
514 static scoped_ptr<DocumentFeed> ExtractAndParse(const base::Value& value);
515
516 // Creates feed from parsed JSON Value. You should call this
517 // instead of instantiating JSONValueConverter by yourself because
518 // this method does some post-process for some fields. See
519 // FillRemainingFields comment and implementation in DocumentEntry
520 // class for the details.
521 static scoped_ptr<DocumentFeed> CreateFrom(const base::Value& value);
522
523 // Registers the mapping between JSON field names and the members in
524 // this class.
525 static void RegisterJSONConverter(
526 base::JSONValueConverter<DocumentFeed>* converter);
527
528 // Returns true and passes|url| of the next feed if the current entry list
529 // does not completed this feed.
530 bool GetNextFeedURL(GURL* url);
531
532 // List of document entries.
533 const ScopedVector<DocumentEntry>& entries() const { return entries_; }
534
535 // Start index of the document entry list.
536 int start_index() const { return start_index_; }
537
538 // Number of items per feed of the document entry list.
539 int items_per_page() const { return items_per_page_; }
540
541 // The largest changestamp. Next time the documents should be fetched
542 // from this changestamp.
543 int largest_changestamp() const { return largest_changestamp_; }
544
545 // Document entry list title.
546 const std::string& title() { return title_; }
547
548 private:
549 DocumentFeed();
550
551 // Parses and initializes data members from content of |value|.
552 // Return false if parsing fails.
553 bool Parse(const base::Value& value);
554
555 ScopedVector<DocumentEntry> entries_;
556 int start_index_;
557 int items_per_page_;
558 std::string title_;
559 int largest_changestamp_;
560
561 DISALLOW_COPY_AND_ASSIGN(DocumentFeed);
562 };
563
564 // Metadata representing installed Google Drive application.
565 class InstalledApp {
566 public:
567 typedef std::vector<std::pair<int, GURL> > IconList;
568
569 InstalledApp();
570 virtual ~InstalledApp();
571
572 // WebApp name.
573 const string16& app_name() const { return app_name_; }
574
575 // Drive app id
576 const std::string& app_id() const { return app_id_; }
577
578 // Object (file) type name that is generated by this WebApp.
579 const string16& object_type() const { return object_type_; }
580
581 // True if WebApp supports creation of new file instances.
582 bool supports_create() const { return supports_create_; }
583
584 // List of primary mime types supported by this WebApp. Primary status should
585 // trigger this WebApp becoming the default handler of file instances that
586 // have these mime types.
587 const ScopedVector<std::string>& primary_mimetypes() const {
588 return primary_mimetypes_;
589 }
590
591 // List of secondary mime types supported by this WebApp. Secondary status
592 // should make this WebApp show up in "Open with..." pop-up menu of the
593 // default action menu for file with matching mime types.
594 const ScopedVector<std::string>& secondary_mimetypes() const {
595 return secondary_mimetypes_;
596 }
597
598 // List of primary file extensions supported by this WebApp. Primary status
599 // should trigger this WebApp becoming the default handler of file instances
600 // that match these extensions.
601 const ScopedVector<std::string>& primary_extensions() const {
602 return primary_extensions_;
603 }
604
605 // List of secondary file extensions supported by this WebApp. Secondary
606 // status should make this WebApp show up in "Open with..." pop-up menu of the
607 // default action menu for file with matching extensions.
608 const ScopedVector<std::string>& secondary_extensions() const {
609 return secondary_extensions_;
610 }
611
612 // List of entry links.
613 const ScopedVector<Link>& links() const { return links_; }
614
615 // Returns a list of icons associated with this installed application.
616 const ScopedVector<AppIcon>& app_icons() const {
617 return app_icons_;
618 }
619
620 // Convenience function for getting the icon URLs for a particular |category|
621 // of icon. Icons are returned in a sorted list, from smallest to largest.
622 IconList GetIconsForCategory(AppIcon::IconCategory category) const;
623
624 // Retrieves product URL from the link collection.
625 GURL GetProductUrl() const;
626
627 // Registers the mapping between JSON field names and the members in
628 // this class.
629 static void RegisterJSONConverter(
630 base::JSONValueConverter<InstalledApp>* converter);
631
632 private:
633 // Extracts "$t" value from the dictionary |value| and returns it in |result|.
634 // If the string value can't be found, it returns false.
635 static bool GetValueString(const base::Value* value,
636 std::string* result);
637
638 std::string app_id_;
639 string16 app_name_;
640 string16 object_type_;
641 bool supports_create_;
642 ScopedVector<std::string> primary_mimetypes_;
643 ScopedVector<std::string> secondary_mimetypes_;
644 ScopedVector<std::string> primary_extensions_;
645 ScopedVector<std::string> secondary_extensions_;
646 ScopedVector<Link> links_;
647 ScopedVector<AppIcon> app_icons_;
648 };
649
650 // Account metadata feed represents the metadata object attached to the user's
651 // account.
652 class AccountMetadataFeed {
653 public:
654 virtual ~AccountMetadataFeed();
655
656 // Creates feed from parsed JSON Value. You should call this
657 // instead of instantiating JSONValueConverter by yourself because
658 // this method does some post-process for some fields. See
659 // FillRemainingFields comment and implementation in DocumentEntry
660 // class for the details.
661 static scoped_ptr<AccountMetadataFeed> CreateFrom(const base::Value& value);
662
663 int64 quota_bytes_total() const {
664 return quota_bytes_total_;
665 }
666
667 int64 quota_bytes_used() const {
668 return quota_bytes_used_;
669 }
670
671 int largest_changestamp() const {
672 return largest_changestamp_;
673 }
674
675 const ScopedVector<InstalledApp>& installed_apps() const {
676 return installed_apps_;
677 }
678
679 // Registers the mapping between JSON field names and the members in
680 // this class.
681 static void RegisterJSONConverter(
682 base::JSONValueConverter<AccountMetadataFeed>* converter);
683
684 private:
685 AccountMetadataFeed();
686
687 // Parses and initializes data members from content of |value|.
688 // Return false if parsing fails.
689 bool Parse(const base::Value& value);
690
691 int64 quota_bytes_total_;
692 int64 quota_bytes_used_;
693 int largest_changestamp_;
694 ScopedVector<InstalledApp> installed_apps_;
695
696 DISALLOW_COPY_AND_ASSIGN(AccountMetadataFeed);
697 };
698
699
700 } // namespace gdata
701
702 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_PARSER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698