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.
5 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_
6 #define GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/strings/string_piece.h"
15 #include "base/time/time.h"
20 template <class StructType
>
21 class JSONValueConverter
;
24 template <class NestedType
>
25 class RepeatedMessageConverter
;
26 } // namespace internal
29 namespace google_apis
{
31 // About resource represents the account information about the current user.
32 // https://developers.google.com/drive/v2/reference/about
38 // Registers the mapping between JSON field names and the members in this
40 static void RegisterJSONConverter(
41 base::JSONValueConverter
<AboutResource
>* converter
);
43 // Creates about resource from parsed JSON.
44 static scoped_ptr
<AboutResource
> CreateFrom(const base::Value
& value
);
46 // Returns the largest change ID number.
47 int64
largest_change_id() const { return largest_change_id_
; }
48 // Returns total number of quota bytes.
49 int64
quota_bytes_total() const { return quota_bytes_total_
; }
50 // Returns the number of quota bytes used.
51 int64
quota_bytes_used_aggregate() const {
52 return quota_bytes_used_aggregate_
;
54 // Returns root folder ID.
55 const std::string
& root_folder_id() const { return root_folder_id_
; }
57 void set_largest_change_id(int64 largest_change_id
) {
58 largest_change_id_
= largest_change_id
;
60 void set_quota_bytes_total(int64 quota_bytes_total
) {
61 quota_bytes_total_
= quota_bytes_total
;
63 void set_quota_bytes_used_aggregate(int64 quota_bytes_used_aggregate
) {
64 quota_bytes_used_aggregate_
= quota_bytes_used_aggregate
;
66 void set_root_folder_id(const std::string
& root_folder_id
) {
67 root_folder_id_
= root_folder_id
;
71 friend class DriveAPIParserTest
;
72 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest
, AboutResourceParser
);
74 // Parses and initializes data members from content of |value|.
75 // Return false if parsing fails.
76 bool Parse(const base::Value
& value
);
78 int64 largest_change_id_
;
79 int64 quota_bytes_total_
;
80 int64 quota_bytes_used_aggregate_
;
81 std::string root_folder_id_
;
83 // This class is copyable on purpose.
86 // DriveAppIcon represents an icon for Drive Application.
87 // https://developers.google.com/drive/v2/reference/apps
91 UNKNOWN
, // Uninitialized state.
92 DOCUMENT
, // Icon for a file associated with the app.
93 APPLICATION
, // Icon for the application.
94 SHARED_DOCUMENT
, // Icon for a shared file associated with the app.
100 // Registers the mapping between JSON field names and the members in this
102 static void RegisterJSONConverter(
103 base::JSONValueConverter
<DriveAppIcon
>* converter
);
105 // Creates drive app icon instance from parsed JSON.
106 static scoped_ptr
<DriveAppIcon
> CreateFrom(const base::Value
& value
);
108 // Category of the icon.
109 IconCategory
category() const { return category_
; }
111 // Size in pixels of one side of the icon (icons are always square).
112 int icon_side_length() const { return icon_side_length_
; }
114 // Returns URL for this icon.
115 const GURL
& icon_url() const { return icon_url_
; }
117 void set_category(IconCategory category
) {
118 category_
= category
;
120 void set_icon_side_length(int icon_side_length
) {
121 icon_side_length_
= icon_side_length
;
123 void set_icon_url(const GURL
& icon_url
) {
124 icon_url_
= icon_url
;
128 // Parses and initializes data members from content of |value|.
129 // Return false if parsing fails.
130 bool Parse(const base::Value
& value
);
132 // Extracts the icon category from the given string. Returns false and does
133 // not change |result| when |scheme| has an unrecognizable value.
134 static bool GetIconCategory(const base::StringPiece
& category
,
135 IconCategory
* result
);
137 friend class base::internal::RepeatedMessageConverter
<DriveAppIcon
>;
138 friend class AppResource
;
140 IconCategory category_
;
141 int icon_side_length_
;
144 DISALLOW_COPY_AND_ASSIGN(DriveAppIcon
);
147 // AppResource represents a Drive Application.
148 // https://developers.google.com/drive/v2/reference/apps
154 // Registers the mapping between JSON field names and the members in this
156 static void RegisterJSONConverter(
157 base::JSONValueConverter
<AppResource
>* converter
);
159 // Creates app resource from parsed JSON.
160 static scoped_ptr
<AppResource
> CreateFrom(const base::Value
& value
);
162 // Returns application ID, which is 12-digit decimals (e.g. "123456780123").
163 const std::string
& application_id() const { return application_id_
; }
165 // Returns application name.
166 const std::string
& name() const { return name_
; }
168 // Returns the name of the type of object this application creates.
169 // This is used for displaying in "Create" menu item for this app.
170 // If empty, application name is used instead.
171 const std::string
& object_type() const { return object_type_
; }
173 // Returns the product ID.
174 const std::string
& product_id() const { return product_id_
; }
176 // Returns whether this application supports creating new objects.
177 bool supports_create() const { return supports_create_
; }
179 // Returns whether this application is removable by apps.delete API.
180 bool is_removable() const { return removable_
; }
182 // Returns the create URL, i.e., the URL for opening a new file by the app.
183 const GURL
& create_url() const { return create_url_
; }
185 // List of primary mime types supported by this WebApp. Primary status should
186 // trigger this WebApp becoming the default handler of file instances that
187 // have these mime types.
188 const ScopedVector
<std::string
>& primary_mimetypes() const {
189 return primary_mimetypes_
;
192 // List of secondary mime types supported by this WebApp. Secondary status
193 // should make this WebApp show up in "Open with..." pop-up menu of the
194 // default action menu for file with matching mime types.
195 const ScopedVector
<std::string
>& secondary_mimetypes() const {
196 return secondary_mimetypes_
;
199 // List of primary file extensions supported by this WebApp. Primary status
200 // should trigger this WebApp becoming the default handler of file instances
201 // that match these extensions.
202 const ScopedVector
<std::string
>& primary_file_extensions() const {
203 return primary_file_extensions_
;
206 // List of secondary file extensions supported by this WebApp. Secondary
207 // status should make this WebApp show up in "Open with..." pop-up menu of the
208 // default action menu for file with matching extensions.
209 const ScopedVector
<std::string
>& secondary_file_extensions() const {
210 return secondary_file_extensions_
;
213 // Returns Icons for this application. An application can have multiple
214 // icons for different purpose (application, document, shared document)
216 const ScopedVector
<DriveAppIcon
>& icons() const {
220 void set_application_id(const std::string
& application_id
) {
221 application_id_
= application_id
;
223 void set_name(const std::string
& name
) { name_
= name
; }
224 void set_object_type(const std::string
& object_type
) {
225 object_type_
= object_type
;
227 void set_product_id(const std::string
& id
) { product_id_
= id
; }
228 void set_supports_create(bool supports_create
) {
229 supports_create_
= supports_create
;
231 void set_removable(bool removable
) { removable_
= removable
; }
232 void set_primary_mimetypes(
233 ScopedVector
<std::string
> primary_mimetypes
) {
234 primary_mimetypes_
= primary_mimetypes
.Pass();
236 void set_secondary_mimetypes(
237 ScopedVector
<std::string
> secondary_mimetypes
) {
238 secondary_mimetypes_
= secondary_mimetypes
.Pass();
240 void set_primary_file_extensions(
241 ScopedVector
<std::string
> primary_file_extensions
) {
242 primary_file_extensions_
= primary_file_extensions
.Pass();
244 void set_secondary_file_extensions(
245 ScopedVector
<std::string
> secondary_file_extensions
) {
246 secondary_file_extensions_
= secondary_file_extensions
.Pass();
248 void set_icons(ScopedVector
<DriveAppIcon
> icons
) {
249 icons_
= icons
.Pass();
251 void set_create_url(const GURL
& url
) {
256 friend class base::internal::RepeatedMessageConverter
<AppResource
>;
257 friend class AppList
;
259 // Parses and initializes data members from content of |value|.
260 // Return false if parsing fails.
261 bool Parse(const base::Value
& value
);
263 std::string application_id_
;
265 std::string object_type_
;
266 std::string product_id_
;
267 bool supports_create_
;
270 ScopedVector
<std::string
> primary_mimetypes_
;
271 ScopedVector
<std::string
> secondary_mimetypes_
;
272 ScopedVector
<std::string
> primary_file_extensions_
;
273 ScopedVector
<std::string
> secondary_file_extensions_
;
274 ScopedVector
<DriveAppIcon
> icons_
;
276 DISALLOW_COPY_AND_ASSIGN(AppResource
);
279 // AppList represents a list of Drive Applications.
280 // https://developers.google.com/drive/v2/reference/apps/list
286 // Registers the mapping between JSON field names and the members in this
288 static void RegisterJSONConverter(
289 base::JSONValueConverter
<AppList
>* converter
);
291 // Creates app list from parsed JSON.
292 static scoped_ptr
<AppList
> CreateFrom(const base::Value
& value
);
294 // ETag for this resource.
295 const std::string
& etag() const { return etag_
; }
297 // Returns a vector of applications.
298 const ScopedVector
<AppResource
>& items() const { return items_
; }
300 void set_etag(const std::string
& etag
) {
303 void set_items(ScopedVector
<AppResource
> items
) {
304 items_
= items
.Pass();
308 friend class DriveAPIParserTest
;
309 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest
, AppListParser
);
311 // Parses and initializes data members from content of |value|.
312 // Return false if parsing fails.
313 bool Parse(const base::Value
& value
);
316 ScopedVector
<AppResource
> items_
;
318 DISALLOW_COPY_AND_ASSIGN(AppList
);
321 // ParentReference represents a directory.
322 // https://developers.google.com/drive/v2/reference/parents
323 class ParentReference
{
328 // Registers the mapping between JSON field names and the members in this
330 static void RegisterJSONConverter(
331 base::JSONValueConverter
<ParentReference
>* converter
);
333 // Creates parent reference from parsed JSON.
334 static scoped_ptr
<ParentReference
> CreateFrom(const base::Value
& value
);
336 // Returns the file id of the reference.
337 const std::string
& file_id() const { return file_id_
; }
339 // Returns the URL for the parent in Drive.
340 const GURL
& parent_link() const { return parent_link_
; }
342 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
343 void set_parent_link(const GURL
& parent_link
) {
344 parent_link_
= parent_link
;
348 // Parses and initializes data members from content of |value|.
349 // Return false if parsing fails.
350 bool Parse(const base::Value
& value
);
352 std::string file_id_
;
356 // FileLabels represents labels for file or folder.
357 // https://developers.google.com/drive/v2/reference/files
363 // Registers the mapping between JSON field names and the members in this
365 static void RegisterJSONConverter(
366 base::JSONValueConverter
<FileLabels
>* converter
);
368 // Creates about resource from parsed JSON.
369 static scoped_ptr
<FileLabels
> CreateFrom(const base::Value
& value
);
371 // Whether this file has been trashed.
372 bool is_trashed() const { return trashed_
; }
374 void set_trashed(bool trashed
) { trashed_
= trashed
; }
377 friend class FileResource
;
379 // Parses and initializes data members from content of |value|.
380 // Return false if parsing fails.
381 bool Parse(const base::Value
& value
);
386 // ImageMediaMetadata represents image metadata for a file.
387 // https://developers.google.com/drive/v2/reference/files
388 class ImageMediaMetadata
{
390 ImageMediaMetadata();
391 ~ImageMediaMetadata();
393 // Registers the mapping between JSON field names and the members in this
395 static void RegisterJSONConverter(
396 base::JSONValueConverter
<ImageMediaMetadata
>* converter
);
398 // Creates about resource from parsed JSON.
399 static scoped_ptr
<ImageMediaMetadata
> CreateFrom(const base::Value
& value
);
401 // Width of the image in pixels.
402 int width() const { return width_
; }
403 // Height of the image in pixels.
404 int height() const { return height_
; }
405 // Rotation of the image in clockwise degrees.
406 int rotation() const { return rotation_
; }
408 void set_width(int width
) { width_
= width
; }
409 void set_height(int height
) { height_
= height
; }
410 void set_rotation(int rotation
) { rotation_
= rotation
; }
413 friend class FileResource
;
415 // Parses and initializes data members from content of |value|.
416 // Return false if parsing fails.
417 bool Parse(const base::Value
& value
);
424 // FileResource represents a file or folder metadata in Drive.
425 // https://developers.google.com/drive/v2/reference/files
428 // Link to open a file resource on a web app with |app_id|.
429 struct OpenWithLink
{
437 // Registers the mapping between JSON field names and the members in this
439 static void RegisterJSONConverter(
440 base::JSONValueConverter
<FileResource
>* converter
);
442 // Creates file resource from parsed JSON.
443 static scoped_ptr
<FileResource
> CreateFrom(const base::Value
& value
);
445 // Returns true if this is a directory.
446 // Note: "folder" is used elsewhere in this file to match Drive API reference,
447 // but outside this file we use "directory" to match HTML5 filesystem API.
448 bool IsDirectory() const;
450 // Returns true if this is a hosted document.
451 // A hosted document is a document in one of Google Docs formats (Documents,
452 // Spreadsheets, Slides, ...) whose content is not exposed via the API. It is
453 // available only as |alternate_link()| to the document hosted on the server.
454 bool IsHostedDocument() const;
456 // Returns file ID. This is unique in all files in Google Drive.
457 const std::string
& file_id() const { return file_id_
; }
459 // Returns ETag for this file.
460 const std::string
& etag() const { return etag_
; }
462 // Returns the title of this file.
463 const std::string
& title() const { return title_
; }
465 // Returns MIME type of this file.
466 const std::string
& mime_type() const { return mime_type_
; }
468 // Returns labels for this file.
469 const FileLabels
& labels() const { return labels_
; }
471 // Returns image media metadata for this file.
472 const ImageMediaMetadata
& image_media_metadata() const {
473 return image_media_metadata_
;
476 // Returns created time of this file.
477 const base::Time
& created_date() const { return created_date_
; }
479 // Returns modified time of this file.
480 const base::Time
& modified_date() const { return modified_date_
; }
482 // Returns last access time by the user.
483 const base::Time
& last_viewed_by_me_date() const {
484 return last_viewed_by_me_date_
;
487 // Returns time when the file was shared with the user.
488 const base::Time
& shared_with_me_date() const {
489 return shared_with_me_date_
;
492 // Returns the 'shared' attribute of the file.
493 bool shared() const { return shared_
; }
495 // Returns MD5 checksum of this file.
496 const std::string
& md5_checksum() const { return md5_checksum_
; }
498 // Returns the size of this file in bytes.
499 int64
file_size() const { return file_size_
; }
501 // Return the link to open the file in Google editor or viewer.
502 // E.g. Google Document, Google Spreadsheet.
503 const GURL
& alternate_link() const { return alternate_link_
; }
505 // Returns URL to the share dialog UI.
506 const GURL
& share_link() const { return share_link_
; }
508 // Returns parent references (directories) of this file.
509 const std::vector
<ParentReference
>& parents() const { return parents_
; }
511 // Returns the list of links to open the resource with a web app.
512 const std::vector
<OpenWithLink
>& open_with_links() const {
513 return open_with_links_
;
516 void set_file_id(const std::string
& file_id
) {
519 void set_etag(const std::string
& etag
) {
522 void set_title(const std::string
& title
) {
525 void set_mime_type(const std::string
& mime_type
) {
526 mime_type_
= mime_type
;
528 FileLabels
* mutable_labels() {
531 ImageMediaMetadata
* mutable_image_media_metadata() {
532 return &image_media_metadata_
;
534 void set_created_date(const base::Time
& created_date
) {
535 created_date_
= created_date
;
537 void set_modified_date(const base::Time
& modified_date
) {
538 modified_date_
= modified_date
;
540 void set_last_viewed_by_me_date(const base::Time
& last_viewed_by_me_date
) {
541 last_viewed_by_me_date_
= last_viewed_by_me_date
;
543 void set_shared_with_me_date(const base::Time
& shared_with_me_date
) {
544 shared_with_me_date_
= shared_with_me_date
;
546 void set_shared(bool shared
) {
549 void set_md5_checksum(const std::string
& md5_checksum
) {
550 md5_checksum_
= md5_checksum
;
552 void set_file_size(int64 file_size
) {
553 file_size_
= file_size
;
555 void set_alternate_link(const GURL
& alternate_link
) {
556 alternate_link_
= alternate_link
;
558 void set_share_link(const GURL
& share_link
) {
559 share_link_
= share_link
;
561 std::vector
<ParentReference
>* mutable_parents() { return &parents_
; }
562 std::vector
<OpenWithLink
>* mutable_open_with_links() {
563 return &open_with_links_
;
567 friend class base::internal::RepeatedMessageConverter
<FileResource
>;
568 friend class ChangeResource
;
569 friend class FileList
;
571 // Parses and initializes data members from content of |value|.
572 // Return false if parsing fails.
573 bool Parse(const base::Value
& value
);
575 std::string file_id_
;
578 std::string mime_type_
;
580 ImageMediaMetadata image_media_metadata_
;
581 base::Time created_date_
;
582 base::Time modified_date_
;
583 base::Time last_viewed_by_me_date_
;
584 base::Time shared_with_me_date_
;
586 std::string md5_checksum_
;
588 GURL alternate_link_
;
590 std::vector
<ParentReference
> parents_
;
591 std::vector
<OpenWithLink
> open_with_links_
;
594 // FileList represents a collection of files and folders.
595 // https://developers.google.com/drive/v2/reference/files/list
601 // Registers the mapping between JSON field names and the members in this
603 static void RegisterJSONConverter(
604 base::JSONValueConverter
<FileList
>* converter
);
606 // Returns true if the |value| has kind field for FileList.
607 static bool HasFileListKind(const base::Value
& value
);
609 // Creates file list from parsed JSON.
610 static scoped_ptr
<FileList
> CreateFrom(const base::Value
& value
);
612 // Returns a link to the next page of files. The URL includes the next page
614 const GURL
& next_link() const { return next_link_
; }
616 // Returns a set of files in this list.
617 const ScopedVector
<FileResource
>& items() const { return items_
; }
618 ScopedVector
<FileResource
>* mutable_items() { return &items_
; }
620 void set_next_link(const GURL
& next_link
) {
621 next_link_
= next_link
;
625 friend class DriveAPIParserTest
;
626 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest
, FileListParser
);
628 // Parses and initializes data members from content of |value|.
629 // Return false if parsing fails.
630 bool Parse(const base::Value
& value
);
633 ScopedVector
<FileResource
> items_
;
635 DISALLOW_COPY_AND_ASSIGN(FileList
);
638 // ChangeResource represents a change in a file.
639 // https://developers.google.com/drive/v2/reference/changes
640 class ChangeResource
{
645 // Registers the mapping between JSON field names and the members in this
647 static void RegisterJSONConverter(
648 base::JSONValueConverter
<ChangeResource
>* converter
);
650 // Creates change resource from parsed JSON.
651 static scoped_ptr
<ChangeResource
> CreateFrom(const base::Value
& value
);
653 // Returns change ID for this change. This is a monotonically increasing
655 int64
change_id() const { return change_id_
; }
657 // Returns a string file ID for corresponding file of the change.
658 const std::string
& file_id() const { return file_id_
; }
660 // Returns true if this file is deleted in the change.
661 bool is_deleted() const { return deleted_
; }
663 // Returns FileResource of the file which the change refers to.
664 const FileResource
* file() const { return file_
.get(); }
665 FileResource
* mutable_file() { return file_
.get(); }
667 // Returns the time of this modification.
668 const base::Time
& modification_date() const { return modification_date_
; }
670 void set_change_id(int64 change_id
) {
671 change_id_
= change_id
;
673 void set_file_id(const std::string
& file_id
) {
676 void set_deleted(bool deleted
) {
679 void set_file(scoped_ptr
<FileResource
> file
) {
682 void set_modification_date(const base::Time
& modification_date
) {
683 modification_date_
= modification_date
;
687 friend class base::internal::RepeatedMessageConverter
<ChangeResource
>;
688 friend class ChangeList
;
690 // Parses and initializes data members from content of |value|.
691 // Return false if parsing fails.
692 bool Parse(const base::Value
& value
);
695 std::string file_id_
;
697 scoped_ptr
<FileResource
> file_
;
698 base::Time modification_date_
;
700 DISALLOW_COPY_AND_ASSIGN(ChangeResource
);
703 // ChangeList represents a set of changes in the drive.
704 // https://developers.google.com/drive/v2/reference/changes/list
710 // Registers the mapping between JSON field names and the members in this
712 static void RegisterJSONConverter(
713 base::JSONValueConverter
<ChangeList
>* converter
);
715 // Returns true if the |value| has kind field for ChangeList.
716 static bool HasChangeListKind(const base::Value
& value
);
718 // Creates change list from parsed JSON.
719 static scoped_ptr
<ChangeList
> CreateFrom(const base::Value
& value
);
721 // Returns a link to the next page of files. The URL includes the next page
723 const GURL
& next_link() const { return next_link_
; }
725 // Returns the largest change ID number.
726 int64
largest_change_id() const { return largest_change_id_
; }
728 // Returns a set of changes in this list.
729 const ScopedVector
<ChangeResource
>& items() const { return items_
; }
730 ScopedVector
<ChangeResource
>* mutable_items() { return &items_
; }
732 void set_next_link(const GURL
& next_link
) {
733 next_link_
= next_link
;
735 void set_largest_change_id(int64 largest_change_id
) {
736 largest_change_id_
= largest_change_id
;
740 friend class DriveAPIParserTest
;
741 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest
, ChangeListParser
);
743 // Parses and initializes data members from content of |value|.
744 // Return false if parsing fails.
745 bool Parse(const base::Value
& value
);
748 int64 largest_change_id_
;
749 ScopedVector
<ChangeResource
> items_
;
751 DISALLOW_COPY_AND_ASSIGN(ChangeList
);
754 } // namespace google_apis
756 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_