Home | History | Annotate | Download | only in google_apis
      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_GOOGLE_APIS_GDATA_WAPI_PARSER_H_
      6 #define CHROME_BROWSER_GOOGLE_APIS_GDATA_WAPI_PARSER_H_
      7 
      8 #include <string>
      9 #include <utility>
     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/strings/string_piece.h"
     16 #include "base/time/time.h"
     17 #include "chrome/browser/google_apis/drive_entry_kinds.h"
     18 #include "url/gurl.h"
     19 
     20 namespace base {
     21 class FilePath;
     22 class DictionaryValue;
     23 class Value;
     24 
     25 template <class StructType>
     26 class JSONValueConverter;
     27 
     28 namespace internal {
     29 template <class NestedType>
     30 class RepeatedMessageConverter;
     31 }  // namespace internal
     32 
     33 }  // namespace base
     34 
     35 // Defines data elements of Google Documents API as described in
     36 // http://code.google.com/apis/documents/.
     37 namespace google_apis {
     38 
     39 // TODO(kochi): These forward declarations will be unnecessary once
     40 // http://crbug.com/142293 is resolved.
     41 class ChangeList;
     42 class ChangeResource;
     43 class FileList;
     44 class FileResource;
     45 
     46 // Defines link (URL) of an entity (document, file, feed...). Each entity could
     47 // have more than one link representing it.
     48 class Link {
     49  public:
     50   enum LinkType {
     51     LINK_UNKNOWN,
     52     LINK_SELF,
     53     LINK_NEXT,
     54     LINK_PARENT,
     55     LINK_ALTERNATE,
     56     LINK_EDIT,
     57     LINK_EDIT_MEDIA,
     58     LINK_ALT_EDIT_MEDIA,
     59     LINK_ALT_POST,
     60     LINK_FEED,
     61     LINK_POST,
     62     LINK_BATCH,
     63     LINK_RESUMABLE_EDIT_MEDIA,
     64     LINK_RESUMABLE_CREATE_MEDIA,
     65     LINK_TABLES_FEED,
     66     LINK_WORKSHEET_FEED,
     67     LINK_THUMBNAIL,
     68     LINK_EMBED,
     69     LINK_PRODUCT,
     70     LINK_ICON,
     71     LINK_OPEN_WITH,
     72     LINK_SHARE,
     73   };
     74   Link();
     75   ~Link();
     76 
     77   // Registers the mapping between JSON field names and the members in
     78   // this class.
     79   static void RegisterJSONConverter(base::JSONValueConverter<Link>* converter);
     80 
     81   // Type of the link.
     82   LinkType type() const { return type_; }
     83 
     84   // URL of the link.
     85   const GURL& href() const { return href_; }
     86 
     87   // Title of the link.
     88   const std::string& title() const { return title_; }
     89 
     90   // For OPEN_WITH links, this contains the application ID. For all other link
     91   // types, it is the empty string.
     92   const std::string& app_id() const { return app_id_; }
     93 
     94   // Link MIME type.
     95   const std::string& mime_type() const { return mime_type_; }
     96 
     97   void set_type(LinkType type) { type_ = type; }
     98   void set_href(const GURL& href) { href_ = href; }
     99   void set_title(const std::string& title) { title_ = title; }
    100   void set_app_id(const std::string& app_id) { app_id_ = app_id; }
    101   void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
    102 
    103  private:
    104   friend class ResourceEntry;
    105   // Converts value of link.rel into LinkType. Outputs to |type| and returns
    106   // true when |rel| has a valid value. Otherwise does nothing and returns
    107   // false.
    108   static bool GetLinkType(const base::StringPiece& rel, LinkType* type);
    109 
    110   // Converts value of link.rel to application ID, if there is one embedded in
    111   // the link.rel field. Outputs to |app_id| and returns true when |rel| has a
    112   // valid value. Otherwise does nothing and returns false.
    113   static bool GetAppID(const base::StringPiece& rel, std::string* app_id);
    114 
    115   LinkType type_;
    116   GURL href_;
    117   std::string title_;
    118   std::string app_id_;
    119   std::string mime_type_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(Link);
    122 };
    123 
    124 // Feed links define links (URLs) to special list of entries (i.e. list of
    125 // previous document revisions).
    126 class ResourceLink {
    127  public:
    128   enum ResourceLinkType {
    129     FEED_LINK_UNKNOWN,
    130     FEED_LINK_ACL,
    131     FEED_LINK_REVISIONS,
    132   };
    133   ResourceLink();
    134 
    135   // Registers the mapping between JSON field names and the members in
    136   // this class.
    137   static void RegisterJSONConverter(
    138       base::JSONValueConverter<ResourceLink>* converter);
    139 
    140   // MIME type of the feed.
    141   ResourceLinkType type() const { return type_; }
    142 
    143   // URL of the feed.
    144   const GURL& href() const { return href_; }
    145 
    146   void set_type(ResourceLinkType type) { type_ = type; }
    147   void set_href(const GURL& href) { href_ = href; }
    148 
    149  private:
    150   friend class ResourceEntry;
    151   // Converts value of gd$feedLink.rel into ResourceLinkType enum.
    152   // Outputs to |result| and returns true when |rel| has a valid
    153   // value.  Otherwise does nothing and returns false.
    154   static bool GetFeedLinkType(
    155       const base::StringPiece& rel, ResourceLinkType* result);
    156 
    157   ResourceLinkType type_;
    158   GURL href_;
    159 
    160   DISALLOW_COPY_AND_ASSIGN(ResourceLink);
    161 };
    162 
    163 // Author represents an author of an entity.
    164 class Author {
    165  public:
    166   Author();
    167 
    168   // Registers the mapping between JSON field names and the members in
    169   // this class.
    170   static void RegisterJSONConverter(
    171       base::JSONValueConverter<Author>* converter);
    172 
    173   // Getters.
    174   const std::string& name() const { return name_; }
    175   const std::string& email() const { return email_; }
    176 
    177   void set_name(const std::string& name) { name_ = name; }
    178   void set_email(const std::string& email) { email_ = email; }
    179 
    180  private:
    181   friend class ResourceEntry;
    182 
    183   std::string name_;
    184   std::string email_;
    185 
    186   DISALLOW_COPY_AND_ASSIGN(Author);
    187 };
    188 
    189 // Entry category.
    190 class Category {
    191  public:
    192   enum CategoryType {
    193     CATEGORY_UNKNOWN,
    194     CATEGORY_ITEM,
    195     CATEGORY_KIND,
    196     CATEGORY_LABEL,
    197   };
    198 
    199   Category();
    200 
    201   // Registers the mapping between JSON field names and the members in
    202   // this class.
    203   static void RegisterJSONConverter(
    204       base::JSONValueConverter<Category>* converter);
    205 
    206   // Category label.
    207   const std::string& label() const { return label_; }
    208 
    209   // Category type.
    210   CategoryType type() const { return type_; }
    211 
    212   // Category term.
    213   const std::string& term() const { return term_; }
    214 
    215   void set_label(const std::string& label) { label_ = label; }
    216   void set_type(CategoryType type) { type_ = type; }
    217   void set_term(const std::string& term) { term_ = term; }
    218 
    219  private:
    220   friend class ResourceEntry;
    221   // Converts category scheme into CategoryType enum. For example,
    222   // http://schemas.google.com/g/2005#kind => Category::CATEGORY_KIND
    223   // Returns false and does not change |result| when |scheme| has an
    224   // unrecognizable value.
    225   static bool GetCategoryTypeFromScheme(
    226       const base::StringPiece& scheme, CategoryType* result);
    227 
    228   std::string label_;
    229   CategoryType type_;
    230   std::string term_;
    231 
    232   DISALLOW_COPY_AND_ASSIGN(Category);
    233 };
    234 
    235 // Content details of a resource: mime-type, url, and so on.
    236 class Content {
    237  public:
    238   Content();
    239 
    240   // Registers the mapping between JSON field names and the members in
    241   // this class.
    242   static void RegisterJSONConverter(
    243       base::JSONValueConverter<Content>* converter);
    244 
    245   // The URL to download the file content.
    246   // Note that the url can expire, so we'll fetch the latest resource
    247   // entry before starting a download to get the download URL.
    248   const GURL& url() const { return url_; }
    249   const std::string& mime_type() const { return mime_type_; }
    250 
    251   void set_url(const GURL& url) { url_ = url; }
    252   void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
    253 
    254  private:
    255   friend class ResourceEntry;
    256 
    257   GURL url_;
    258   std::string mime_type_;
    259 };
    260 
    261 // This stores a representation of an application icon as registered with the
    262 // installed applications section of the account metadata feed. There can be
    263 // multiple icons registered for each application, differing in size, category
    264 // and MIME type.
    265 class AppIcon {
    266  public:
    267   enum IconCategory {
    268     ICON_UNKNOWN,          // Uninitialized state
    269     ICON_DOCUMENT,         // Document icon for various MIME types
    270     ICON_APPLICATION,      // Application icon for various MIME types
    271     ICON_SHARED_DOCUMENT,  // Icon for documents that are shared from other
    272                            // users.
    273   };
    274 
    275   AppIcon();
    276   ~AppIcon();
    277 
    278   // Registers the mapping between JSON field names and the members in
    279   // this class.
    280   static void RegisterJSONConverter(
    281       base::JSONValueConverter<AppIcon>* converter);
    282 
    283   // Category of the icon.
    284   IconCategory category() const { return category_; }
    285 
    286   // Size in pixels of one side of the icon (icons are always square).
    287   int icon_side_length() const { return icon_side_length_; }
    288 
    289   // Get a list of links available for this AppIcon.
    290   const ScopedVector<Link>& links() const { return links_; }
    291 
    292   // Get the icon URL from the internal list of links.  Returns the first
    293   // icon URL found in the list.
    294   GURL GetIconURL() const;
    295 
    296   void set_category(IconCategory category) { category_ = category; }
    297   void set_icon_side_length(int icon_side_length) {
    298     icon_side_length_ = icon_side_length;
    299   }
    300   void set_links(ScopedVector<Link>* links) { links_.swap(*links); }
    301 
    302  private:
    303   // Extracts the icon category from the given string. Returns false and does
    304   // not change |result| when |scheme| has an unrecognizable value.
    305   static bool GetIconCategory(const base::StringPiece& category,
    306                               IconCategory* result);
    307 
    308   IconCategory category_;
    309   int icon_side_length_;
    310   ScopedVector<Link> links_;
    311 
    312   DISALLOW_COPY_AND_ASSIGN(AppIcon);
    313 };
    314 
    315 // Base class for feed entries. This class defines fields commonly used by
    316 // various feeds.
    317 class CommonMetadata {
    318  public:
    319   CommonMetadata();
    320   virtual ~CommonMetadata();
    321 
    322   // Returns a link of a given |type| for this entry. If not found, it returns
    323   // NULL.
    324   const Link* GetLinkByType(Link::LinkType type) const;
    325 
    326   // Entry update time.
    327   base::Time updated_time() const { return updated_time_; }
    328 
    329   // Entry ETag.
    330   const std::string& etag() const { return etag_; }
    331 
    332   // List of entry authors.
    333   const ScopedVector<Author>& authors() const { return authors_; }
    334 
    335   // List of entry links.
    336   const ScopedVector<Link>& links() const { return links_; }
    337   ScopedVector<Link>* mutable_links() { return &links_; }
    338 
    339   // List of entry categories.
    340   const ScopedVector<Category>& categories() const { return categories_; }
    341 
    342   void set_etag(const std::string& etag) { etag_ = etag; }
    343   void set_authors(ScopedVector<Author>* authors) {
    344     authors_.swap(*authors);
    345   }
    346   void set_links(ScopedVector<Link>* links) {
    347     links_.swap(*links);
    348   }
    349   void set_categories(ScopedVector<Category>* categories) {
    350     categories_.swap(*categories);
    351   }
    352   void set_updated_time(const base::Time& updated_time) {
    353     updated_time_ = updated_time;
    354   }
    355 
    356  protected:
    357   // Registers the mapping between JSON field names and the members in
    358   // this class.
    359   template<typename CommonMetadataDescendant>
    360   static void RegisterJSONConverter(
    361       base::JSONValueConverter<CommonMetadataDescendant>* converter);
    362 
    363   std::string etag_;
    364   ScopedVector<Author> authors_;
    365   ScopedVector<Link> links_;
    366   ScopedVector<Category> categories_;
    367   base::Time updated_time_;
    368 
    369   DISALLOW_COPY_AND_ASSIGN(CommonMetadata);
    370 };
    371 
    372 // This class represents a resource entry. A resource is a generic term which
    373 // refers to a file and a directory.
    374 class ResourceEntry : public CommonMetadata {
    375  public:
    376   ResourceEntry();
    377   virtual ~ResourceEntry();
    378 
    379   // Extracts "entry" dictionary from the JSON value, and parse the contents,
    380   // using CreateFrom(). Returns NULL on failure. The input JSON data, coming
    381   // from the gdata server, looks like:
    382   //
    383   // {
    384   //   "encoding": "UTF-8",
    385   //   "entry": { ... },   // This function will extract this and parse.
    386   //   "version": "1.0"
    387   // }
    388   //
    389   // The caller should delete the returned object.
    390   static scoped_ptr<ResourceEntry> ExtractAndParse(const base::Value& value);
    391 
    392   // Creates resource entry from parsed JSON Value.  You should call
    393   // this instead of instantiating JSONValueConverter by yourself
    394   // because this method does some post-process for some fields.  See
    395   // FillRemainingFields comment and implementation for the details.
    396   static scoped_ptr<ResourceEntry> CreateFrom(const base::Value& value);
    397 
    398   // Creates resource entry from FileResource.
    399   // TODO(kochi): This should go away soon. http://crbug.com/142293
    400   static scoped_ptr<ResourceEntry> CreateFromFileResource(
    401       const FileResource& file);
    402 
    403   // Creates resource entry from ChangeResource.
    404   // Todo(Kochi): This should go away soon. http://crbug.com/142293
    405   static scoped_ptr<ResourceEntry> CreateFromChangeResource(
    406       const ChangeResource& change);
    407 
    408   // Returns name of entry node.
    409   static std::string GetEntryNodeName();
    410 
    411   // Registers the mapping between JSON field names and the members in
    412   // this class.
    413   static void RegisterJSONConverter(
    414       base::JSONValueConverter<ResourceEntry>* converter);
    415 
    416   // Sets true to |result| if the field exists.
    417   // Always returns true even when the field does not exist.
    418   static bool HasFieldPresent(const base::Value* value, bool* result);
    419 
    420   // Parses |value| as int64 and sets it to |result|. If the field does not
    421   // exist, sets 0 to |result| as default value.
    422   // Returns true if |value| is NULL or it is parsed as int64 successfully.
    423   static bool ParseChangestamp(const base::Value* value, int64* result);
    424 
    425   // The resource ID is used to identify a resource, which looks like:
    426   // file:d41d8cd98f00b204e9800998ecf8
    427   const std::string& resource_id() const { return resource_id_; }
    428 
    429   // This is a URL looks like:
    430   // https://docs.google.com/feeds/id/file%3Ad41d8cd98f00b204e9800998ecf8.
    431   // The URL is currently not used.
    432   const std::string& id() const { return id_; }
    433 
    434   DriveEntryKind kind() const { return kind_; }
    435   const std::string& title() const { return title_; }
    436   base::Time published_time() const { return published_time_; }
    437   base::Time last_viewed_time() const { return last_viewed_time_; }
    438   const std::vector<std::string>& labels() const { return labels_; }
    439 
    440   // The URL to download a file content.
    441   // Search for 'download_url' in gdata_wapi_requests.h for details.
    442   const GURL& download_url() const { return content_.url(); }
    443 
    444   const std::string& content_mime_type() const { return content_.mime_type(); }
    445 
    446   // The resource links contain extra links for revisions and access control,
    447   // etc.  Note that links() contain more basic links like edit URL,
    448   // alternative URL, etc.
    449   const ScopedVector<ResourceLink>& resource_links() const {
    450     return resource_links_;
    451   }
    452 
    453   // File name (exists only for kinds FILE and PDF).
    454   const std::string& filename() const { return filename_; }
    455 
    456   // Suggested file name (exists only for kinds FILE and PDF).
    457   const std::string& suggested_filename() const { return suggested_filename_; }
    458 
    459   // File content MD5 (exists only for kinds FILE and PDF).
    460   const std::string& file_md5() const { return file_md5_; }
    461 
    462   // File size (exists only for kinds FILE and PDF).
    463   int64 file_size() const { return file_size_; }
    464 
    465   // True if the file or directory is deleted (applicable to change list only).
    466   bool deleted() const { return deleted_ || removed_; }
    467 
    468   // Changestamp (exists only for change query results).
    469   // If not exists, defaults to 0.
    470   int64 changestamp() const { return changestamp_; }
    471 
    472   // Text version of resource entry kind. Returns an empty string for
    473   // unknown entry kind.
    474   std::string GetEntryKindText() const;
    475 
    476   // Returns preferred file extension for hosted documents. If entry is not
    477   // a hosted document, this call returns an empty string.
    478   std::string GetHostedDocumentExtension() const;
    479 
    480   // True if resource entry is remotely hosted.
    481   bool is_hosted_document() const {
    482     return (ClassifyEntryKind(kind_) & KIND_OF_HOSTED_DOCUMENT) > 0;
    483   }
    484   // True if resource entry hosted by Google Documents.
    485   bool is_google_document() const {
    486     return (ClassifyEntryKind(kind_) & KIND_OF_GOOGLE_DOCUMENT) > 0;
    487   }
    488   // True if resource entry is hosted by an external application.
    489   bool is_external_document() const {
    490     return (ClassifyEntryKind(kind_) & KIND_OF_EXTERNAL_DOCUMENT) > 0;
    491   }
    492   // True if resource entry is a folder (collection).
    493   bool is_folder() const {
    494     return (ClassifyEntryKind(kind_) & KIND_OF_FOLDER) > 0;
    495   }
    496   // True if resource entry is regular file.
    497   bool is_file() const {
    498     return (ClassifyEntryKind(kind_) & KIND_OF_FILE) > 0;
    499   }
    500   // True if resource entry can't be mapped to the file system.
    501   bool is_special() const {
    502     return !is_file() && !is_folder() && !is_hosted_document();
    503   }
    504 
    505   // The following constructs are exposed for unit tests.
    506 
    507   // Classes of EntryKind. Used for ClassifyEntryKind().
    508   enum EntryKindClass {
    509     KIND_OF_NONE = 0,
    510     KIND_OF_HOSTED_DOCUMENT = 1,
    511     KIND_OF_GOOGLE_DOCUMENT = 1 << 1,
    512     KIND_OF_EXTERNAL_DOCUMENT = 1 << 2,
    513     KIND_OF_FOLDER = 1 << 3,
    514     KIND_OF_FILE = 1 << 4,
    515   };
    516 
    517   // Classifies the EntryKind. The returned value is a bitmask of
    518   // EntryKindClass. For example, DOCUMENT is classified as
    519   // KIND_OF_HOSTED_DOCUMENT and KIND_OF_GOOGLE_DOCUMENT, hence the returned
    520   // value is KIND_OF_HOSTED_DOCUMENT | KIND_OF_GOOGLE_DOCUMENT.
    521   static int ClassifyEntryKind(DriveEntryKind kind);
    522 
    523   // Classifies the EntryKind by the file extension of specific path. The
    524   // returned value is a bitmask of EntryKindClass. See also ClassifyEntryKind.
    525   static int ClassifyEntryKindByFileExtension(const base::FilePath& file);
    526 
    527   void set_resource_id(const std::string& resource_id) {
    528     resource_id_ = resource_id;
    529   }
    530   void set_id(const std::string& id) { id_ = id; }
    531   void set_kind(DriveEntryKind kind) { kind_ = kind; }
    532   void set_title(const std::string& title) { title_ = title; }
    533   void set_published_time(const base::Time& published_time) {
    534     published_time_ = published_time;
    535   }
    536   void set_last_viewed_time(const base::Time& last_viewed_time) {
    537     last_viewed_time_ = last_viewed_time;
    538   }
    539   void set_labels(const std::vector<std::string>& labels) {
    540     labels_ = labels;
    541   }
    542   void set_content(const Content& content) {
    543     content_ = content;
    544   }
    545   void set_resource_links(ScopedVector<ResourceLink>* resource_links) {
    546     resource_links_.swap(*resource_links);
    547   }
    548   void set_filename(const std::string& filename) { filename_ = filename; }
    549   void set_suggested_filename(const std::string& suggested_filename) {
    550     suggested_filename_ = suggested_filename;
    551   }
    552   void set_file_md5(const std::string& file_md5) { file_md5_ = file_md5; }
    553   void set_file_size(int64 file_size) { file_size_ = file_size; }
    554   void set_deleted(bool deleted) { deleted_ = deleted; }
    555   void set_removed(bool removed) { removed_ = removed; }
    556   void set_changestamp(int64 changestamp) { changestamp_ = changestamp; }
    557 
    558  private:
    559   friend class base::internal::RepeatedMessageConverter<ResourceEntry>;
    560   friend class ResourceList;
    561   friend class ResumeUploadRequest;
    562 
    563   // Fills the remaining fields where JSONValueConverter cannot catch.
    564   void FillRemainingFields();
    565 
    566   // Converts categories.term into DriveEntryKind enum.
    567   static DriveEntryKind GetEntryKindFromTerm(const std::string& term);
    568   // Converts |kind| into its text identifier equivalent.
    569   static const char* GetEntryKindDescription(DriveEntryKind kind);
    570 
    571   std::string resource_id_;
    572   std::string id_;
    573   DriveEntryKind kind_;
    574   std::string title_;
    575   base::Time published_time_;
    576   // Last viewed value may be unreliable. See: crbug.com/152628.
    577   base::Time last_viewed_time_;
    578   std::vector<std::string> labels_;
    579   Content content_;
    580   ScopedVector<ResourceLink> resource_links_;
    581   // Optional fields for files only.
    582   std::string filename_;
    583   std::string suggested_filename_;
    584   std::string file_md5_;
    585   int64 file_size_;
    586   bool deleted_;
    587   bool removed_;
    588   int64 changestamp_;
    589 
    590   DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
    591 };
    592 
    593 // This class represents a list of resource entries with some extra metadata
    594 // such as the root upload URL. The feed is paginated and the rest of the
    595 // feed can be fetched by retrieving the remaining parts of the feed from
    596 // URLs provided by GetNextFeedURL() method.
    597 class ResourceList : public CommonMetadata {
    598  public:
    599   ResourceList();
    600   virtual ~ResourceList();
    601 
    602   // Extracts "feed" dictionary from the JSON value, and parse the contents,
    603   // using CreateFrom(). Returns NULL on failure. The input JSON data, coming
    604   // from the gdata server, looks like:
    605   //
    606   // {
    607   //   "encoding": "UTF-8",
    608   //   "feed": { ... },   // This function will extract this and parse.
    609   //   "version": "1.0"
    610   // }
    611   static scoped_ptr<ResourceList> ExtractAndParse(const base::Value& value);
    612 
    613   // Creates feed from parsed JSON Value.  You should call this
    614   // instead of instantiating JSONValueConverter by yourself because
    615   // this method does some post-process for some fields.  See
    616   // FillRemainingFields comment and implementation in ResourceEntry
    617   // class for the details.
    618   static scoped_ptr<ResourceList> CreateFrom(const base::Value& value);
    619   // Variant of CreateFrom() above, creates feed from parsed ChangeList.
    620   // TODO(hidehiko): This should go away soon. http://crbug.com/142293
    621   static scoped_ptr<ResourceList> CreateFromChangeList(
    622       const ChangeList& changelist);
    623   // Variant of CreateFrom() above, creates feed from parsed FileList.
    624   // TODO(hidehiko): This should go away soon. http://crbug.com/142293
    625   static scoped_ptr<ResourceList> CreateFromFileList(
    626       const FileList& file_list);
    627 
    628   // Registers the mapping between JSON field names and the members in
    629   // this class.
    630   static void RegisterJSONConverter(
    631       base::JSONValueConverter<ResourceList>* converter);
    632 
    633   // Returns true and passes|url| of the next feed if the current entry list
    634   // does not completed this feed.
    635   bool GetNextFeedURL(GURL* url) const;
    636 
    637   // List of resource entries.
    638   const ScopedVector<ResourceEntry>& entries() const { return entries_; }
    639   ScopedVector<ResourceEntry>* mutable_entries() { return &entries_; }
    640 
    641   // Releases entries_ into |entries|. This is a transfer of ownership, so the
    642   // caller is responsible for deleting the elements of |entries|.
    643   void ReleaseEntries(std::vector<ResourceEntry*>* entries);
    644 
    645   // Start index of the resource entry list.
    646   int start_index() const { return start_index_; }
    647 
    648   // Number of items per feed of the resource entry list.
    649   int items_per_page() const { return items_per_page_; }
    650 
    651   // The largest changestamp. Next time the resource list should be fetched
    652   // from this changestamp.
    653   int64 largest_changestamp() const { return largest_changestamp_; }
    654 
    655   // Resource entry list title.
    656   const std::string& title() { return title_; }
    657 
    658   void set_entries(ScopedVector<ResourceEntry>* entries) {
    659     entries_.swap(*entries);
    660   }
    661   void set_start_index(int start_index) {
    662     start_index_ = start_index;
    663   }
    664   void set_items_per_page(int items_per_page) {
    665     items_per_page_ = items_per_page;
    666   }
    667   void set_title(const std::string& title) {
    668     title_ = title;
    669   }
    670   void set_largest_changestamp(int64 largest_changestamp) {
    671     largest_changestamp_ = largest_changestamp;
    672   }
    673 
    674  private:
    675   // Parses and initializes data members from content of |value|.
    676   // Return false if parsing fails.
    677   bool Parse(const base::Value& value);
    678 
    679   ScopedVector<ResourceEntry> entries_;
    680   int start_index_;
    681   int items_per_page_;
    682   std::string title_;
    683   int64 largest_changestamp_;
    684 
    685   DISALLOW_COPY_AND_ASSIGN(ResourceList);
    686 };
    687 
    688 // Metadata representing installed Google Drive application.
    689 class InstalledApp {
    690  public:
    691   typedef std::vector<std::pair<int, GURL> > IconList;
    692 
    693   InstalledApp();
    694   virtual ~InstalledApp();
    695 
    696   // WebApp name.
    697   const std::string& app_name() const { return app_name_; }
    698 
    699   // Drive app id
    700   const std::string& app_id() const { return app_id_; }
    701 
    702   // Object (file) type name that is generated by this WebApp.
    703   const std::string& object_type() const { return object_type_; }
    704 
    705   // True if WebApp supports creation of new file instances.
    706   bool supports_create() const { return supports_create_; }
    707 
    708   // List of primary mime types supported by this WebApp. Primary status should
    709   // trigger this WebApp becoming the default handler of file instances that
    710   // have these mime types.
    711   const ScopedVector<std::string>& primary_mimetypes() const {
    712     return primary_mimetypes_;
    713   }
    714 
    715   // List of secondary mime types supported by this WebApp. Secondary status
    716   // should make this WebApp show up in "Open with..." pop-up menu of the
    717   // default action menu for file with matching mime types.
    718   const ScopedVector<std::string>& secondary_mimetypes() const {
    719     return secondary_mimetypes_;
    720   }
    721 
    722   // List of primary file extensions supported by this WebApp. Primary status
    723   // should trigger this WebApp becoming the default handler of file instances
    724   // that match these extensions.
    725   const ScopedVector<std::string>& primary_extensions() const {
    726     return primary_extensions_;
    727   }
    728 
    729   // List of secondary file extensions supported by this WebApp. Secondary
    730   // status should make this WebApp show up in "Open with..." pop-up menu of the
    731   // default action menu for file with matching extensions.
    732   const ScopedVector<std::string>& secondary_extensions() const {
    733     return secondary_extensions_;
    734   }
    735 
    736   // List of entry links.
    737   const ScopedVector<Link>& links() const { return links_; }
    738 
    739   // Returns a list of icons associated with this installed application.
    740   const ScopedVector<AppIcon>& app_icons() const {
    741     return app_icons_;
    742   }
    743 
    744   // Convenience function for getting the icon URLs for a particular |category|
    745   // of icon. Icons are returned in a sorted list, from smallest to largest.
    746   IconList GetIconsForCategory(AppIcon::IconCategory category) const;
    747 
    748   // Retrieves product URL from the link collection.
    749   GURL GetProductUrl() const;
    750 
    751   // Registers the mapping between JSON field names and the members in
    752   // this class.
    753   static void RegisterJSONConverter(
    754       base::JSONValueConverter<InstalledApp>* converter);
    755 
    756   void set_app_id(const std::string& app_id) { app_id_ = app_id; }
    757   void set_app_name(const std::string& app_name) { app_name_ = app_name; }
    758   void set_object_type(const std::string& object_type) {
    759     object_type_ = object_type;
    760   }
    761   void set_supports_create(bool supports_create) {
    762     supports_create_ = supports_create;
    763   }
    764   void set_primary_mimetypes(
    765       ScopedVector<std::string>* primary_mimetypes) {
    766     primary_mimetypes_.swap(*primary_mimetypes);
    767   }
    768   void set_secondary_mimetypes(
    769       ScopedVector<std::string>* secondary_mimetypes) {
    770     secondary_mimetypes_.swap(*secondary_mimetypes);
    771   }
    772   void set_primary_extensions(
    773       ScopedVector<std::string>* primary_extensions) {
    774     primary_extensions_.swap(*primary_extensions);
    775   }
    776   void set_secondary_extensions(
    777       ScopedVector<std::string>* secondary_extensions) {
    778     secondary_extensions_.swap(*secondary_extensions);
    779   }
    780   void set_links(ScopedVector<Link>* links) {
    781     links_.swap(*links);
    782   }
    783   void set_app_icons(ScopedVector<AppIcon>* app_icons) {
    784     app_icons_.swap(*app_icons);
    785   }
    786 
    787  private:
    788   // Extracts "$t" value from the dictionary |value| and returns it in |result|.
    789   // If the string value can't be found, it returns false.
    790   static bool GetValueString(const base::Value* value,
    791                              std::string* result);
    792 
    793   std::string app_id_;
    794   std::string app_name_;
    795   std::string object_type_;
    796   bool supports_create_;
    797   ScopedVector<std::string> primary_mimetypes_;
    798   ScopedVector<std::string> secondary_mimetypes_;
    799   ScopedVector<std::string> primary_extensions_;
    800   ScopedVector<std::string> secondary_extensions_;
    801   ScopedVector<Link> links_;
    802   ScopedVector<AppIcon> app_icons_;
    803 };
    804 
    805 // Account metadata feed represents the metadata object attached to the user's
    806 // account.
    807 class AccountMetadata {
    808  public:
    809   AccountMetadata();
    810   virtual ~AccountMetadata();
    811 
    812   // Creates feed from parsed JSON Value.  You should call this
    813   // instead of instantiating JSONValueConverter by yourself because
    814   // this method does some post-process for some fields.  See
    815   // FillRemainingFields comment and implementation in ResourceEntry
    816   // class for the details.
    817   static scoped_ptr<AccountMetadata> CreateFrom(const base::Value& value);
    818 
    819   int64 quota_bytes_total() const {
    820     return quota_bytes_total_;
    821   }
    822 
    823   int64 quota_bytes_used() const {
    824     return quota_bytes_used_;
    825   }
    826 
    827   int64 largest_changestamp() const {
    828     return largest_changestamp_;
    829   }
    830 
    831   const ScopedVector<InstalledApp>& installed_apps() const {
    832     return installed_apps_;
    833   }
    834 
    835   void set_quota_bytes_total(int64 quota_bytes_total) {
    836     quota_bytes_total_ = quota_bytes_total;
    837   }
    838   void set_quota_bytes_used(int64 quota_bytes_used) {
    839     quota_bytes_used_ = quota_bytes_used;
    840   }
    841   void set_largest_changestamp(int64 largest_changestamp) {
    842     largest_changestamp_ = largest_changestamp;
    843   }
    844   void set_installed_apps(ScopedVector<InstalledApp>* installed_apps) {
    845     installed_apps_.swap(*installed_apps);
    846   }
    847 
    848   // Registers the mapping between JSON field names and the members in
    849   // this class.
    850   static void RegisterJSONConverter(
    851       base::JSONValueConverter<AccountMetadata>* converter);
    852 
    853  private:
    854   // Parses and initializes data members from content of |value|.
    855   // Return false if parsing fails.
    856   bool Parse(const base::Value& value);
    857 
    858   int64 quota_bytes_total_;
    859   int64 quota_bytes_used_;
    860   int64 largest_changestamp_;
    861   ScopedVector<InstalledApp> installed_apps_;
    862 
    863   DISALLOW_COPY_AND_ASSIGN(AccountMetadata);
    864 };
    865 
    866 
    867 }  // namespace google_apis
    868 
    869 #endif  // CHROME_BROWSER_GOOGLE_APIS_GDATA_WAPI_PARSER_H_
    870