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 GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_ 6 #define GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_ 7 8 #include <string> 9 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" 16 #include "url/gurl.h" 17 18 namespace base { 19 class Value; 20 template <class StructType> 21 class JSONValueConverter; 22 23 namespace internal { 24 template <class NestedType> 25 class RepeatedMessageConverter; 26 } // namespace internal 27 } // namespace base 28 29 namespace google_apis { 30 31 // About resource represents the account information about the current user. 32 // https://developers.google.com/drive/v2/reference/about 33 class AboutResource { 34 public: 35 AboutResource(); 36 ~AboutResource(); 37 38 // Registers the mapping between JSON field names and the members in this 39 // class. 40 static void RegisterJSONConverter( 41 base::JSONValueConverter<AboutResource>* converter); 42 43 // Creates about resource from parsed JSON. 44 static scoped_ptr<AboutResource> CreateFrom(const base::Value& value); 45 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() const { return quota_bytes_used_; } 52 // Returns root folder ID. 53 const std::string& root_folder_id() const { return root_folder_id_; } 54 55 void set_largest_change_id(int64 largest_change_id) { 56 largest_change_id_ = largest_change_id; 57 } 58 void set_quota_bytes_total(int64 quota_bytes_total) { 59 quota_bytes_total_ = quota_bytes_total; 60 } 61 void set_quota_bytes_used(int64 quota_bytes_used) { 62 quota_bytes_used_ = quota_bytes_used; 63 } 64 void set_root_folder_id(const std::string& root_folder_id) { 65 root_folder_id_ = root_folder_id; 66 } 67 68 private: 69 friend class DriveAPIParserTest; 70 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, AboutResourceParser); 71 72 // Parses and initializes data members from content of |value|. 73 // Return false if parsing fails. 74 bool Parse(const base::Value& value); 75 76 int64 largest_change_id_; 77 int64 quota_bytes_total_; 78 int64 quota_bytes_used_; 79 std::string root_folder_id_; 80 81 // This class is copyable on purpose. 82 }; 83 84 // DriveAppIcon represents an icon for Drive Application. 85 // https://developers.google.com/drive/v2/reference/apps 86 class DriveAppIcon { 87 public: 88 enum IconCategory { 89 UNKNOWN, // Uninitialized state. 90 DOCUMENT, // Icon for a file associated with the app. 91 APPLICATION, // Icon for the application. 92 SHARED_DOCUMENT, // Icon for a shared file associated with the app. 93 }; 94 95 DriveAppIcon(); 96 ~DriveAppIcon(); 97 98 // Registers the mapping between JSON field names and the members in this 99 // class. 100 static void RegisterJSONConverter( 101 base::JSONValueConverter<DriveAppIcon>* converter); 102 103 // Creates drive app icon instance from parsed JSON. 104 static scoped_ptr<DriveAppIcon> CreateFrom(const base::Value& value); 105 106 // Category of the icon. 107 IconCategory category() const { return category_; } 108 109 // Size in pixels of one side of the icon (icons are always square). 110 int icon_side_length() const { return icon_side_length_; } 111 112 // Returns URL for this icon. 113 const GURL& icon_url() const { return icon_url_; } 114 115 void set_category(IconCategory category) { 116 category_ = category; 117 } 118 void set_icon_side_length(int icon_side_length) { 119 icon_side_length_ = icon_side_length; 120 } 121 void set_icon_url(const GURL& icon_url) { 122 icon_url_ = icon_url; 123 } 124 125 private: 126 // Parses and initializes data members from content of |value|. 127 // Return false if parsing fails. 128 bool Parse(const base::Value& value); 129 130 // Extracts the icon category from the given string. Returns false and does 131 // not change |result| when |scheme| has an unrecognizable value. 132 static bool GetIconCategory(const base::StringPiece& category, 133 IconCategory* result); 134 135 friend class base::internal::RepeatedMessageConverter<DriveAppIcon>; 136 friend class AppResource; 137 138 IconCategory category_; 139 int icon_side_length_; 140 GURL icon_url_; 141 142 DISALLOW_COPY_AND_ASSIGN(DriveAppIcon); 143 }; 144 145 // AppResource represents a Drive Application. 146 // https://developers.google.com/drive/v2/reference/apps 147 class AppResource { 148 public: 149 ~AppResource(); 150 AppResource(); 151 152 // Registers the mapping between JSON field names and the members in this 153 // class. 154 static void RegisterJSONConverter( 155 base::JSONValueConverter<AppResource>* converter); 156 157 // Creates app resource from parsed JSON. 158 static scoped_ptr<AppResource> CreateFrom(const base::Value& value); 159 160 // Returns application ID, which is 12-digit decimals (e.g. "123456780123"). 161 const std::string& application_id() const { return application_id_; } 162 163 // Returns application name. 164 const std::string& name() const { return name_; } 165 166 // Returns the name of the type of object this application creates. 167 // This is used for displaying in "Create" menu item for this app. 168 // If empty, application name is used instead. 169 const std::string& object_type() const { return object_type_; } 170 171 // Returns whether this application supports creating new objects. 172 bool supports_create() const { return supports_create_; } 173 174 // Returns whether this application supports importing Google Docs. 175 bool supports_import() const { return supports_import_; } 176 177 // Returns whether this application is installed. 178 bool is_installed() const { return installed_; } 179 180 // Returns whether this application is authorized to access data on the 181 // user's Drive. 182 bool is_authorized() const { return authorized_; } 183 184 // Returns the product URL, e.g. at Chrome Web Store. 185 const GURL& product_url() const { return product_url_; } 186 187 // List of primary mime types supported by this WebApp. Primary status should 188 // trigger this WebApp becoming the default handler of file instances that 189 // have these mime types. 190 const ScopedVector<std::string>& primary_mimetypes() const { 191 return primary_mimetypes_; 192 } 193 194 // List of secondary mime types supported by this WebApp. Secondary status 195 // should make this WebApp show up in "Open with..." pop-up menu of the 196 // default action menu for file with matching mime types. 197 const ScopedVector<std::string>& secondary_mimetypes() const { 198 return secondary_mimetypes_; 199 } 200 201 // List of primary file extensions supported by this WebApp. Primary status 202 // should trigger this WebApp becoming the default handler of file instances 203 // that match these extensions. 204 const ScopedVector<std::string>& primary_file_extensions() const { 205 return primary_file_extensions_; 206 } 207 208 // List of secondary file extensions supported by this WebApp. Secondary 209 // status should make this WebApp show up in "Open with..." pop-up menu of the 210 // default action menu for file with matching extensions. 211 const ScopedVector<std::string>& secondary_file_extensions() const { 212 return secondary_file_extensions_; 213 } 214 215 // Returns Icons for this application. An application can have multiple 216 // icons for different purpose (application, document, shared document) 217 // in several sizes. 218 const ScopedVector<DriveAppIcon>& icons() const { 219 return icons_; 220 } 221 222 void set_application_id(const std::string& application_id) { 223 application_id_ = application_id; 224 } 225 void set_name(const std::string& name) { name_ = name; } 226 void set_object_type(const std::string& object_type) { 227 object_type_ = object_type; 228 } 229 void set_supports_create(bool supports_create) { 230 supports_create_ = supports_create; 231 } 232 void set_supports_import(bool supports_import) { 233 supports_import_ = supports_import; 234 } 235 void set_installed(bool installed) { installed_ = installed; } 236 void set_authorized(bool authorized) { authorized_ = authorized; } 237 void set_product_url(const GURL& product_url) { 238 product_url_ = product_url; 239 } 240 void set_primary_mimetypes( 241 ScopedVector<std::string> primary_mimetypes) { 242 primary_mimetypes_ = primary_mimetypes.Pass(); 243 } 244 void set_secondary_mimetypes( 245 ScopedVector<std::string> secondary_mimetypes) { 246 secondary_mimetypes_ = secondary_mimetypes.Pass(); 247 } 248 void set_primary_file_extensions( 249 ScopedVector<std::string> primary_file_extensions) { 250 primary_file_extensions_ = primary_file_extensions.Pass(); 251 } 252 void set_secondary_file_extensions( 253 ScopedVector<std::string> secondary_file_extensions) { 254 secondary_file_extensions_ = secondary_file_extensions.Pass(); 255 } 256 void set_icons(ScopedVector<DriveAppIcon> icons) { 257 icons_ = icons.Pass(); 258 } 259 260 private: 261 friend class base::internal::RepeatedMessageConverter<AppResource>; 262 friend class AppList; 263 264 // Parses and initializes data members from content of |value|. 265 // Return false if parsing fails. 266 bool Parse(const base::Value& value); 267 268 std::string application_id_; 269 std::string name_; 270 std::string object_type_; 271 bool supports_create_; 272 bool supports_import_; 273 bool installed_; 274 bool authorized_; 275 GURL product_url_; 276 ScopedVector<std::string> primary_mimetypes_; 277 ScopedVector<std::string> secondary_mimetypes_; 278 ScopedVector<std::string> primary_file_extensions_; 279 ScopedVector<std::string> secondary_file_extensions_; 280 ScopedVector<DriveAppIcon> icons_; 281 282 DISALLOW_COPY_AND_ASSIGN(AppResource); 283 }; 284 285 // AppList represents a list of Drive Applications. 286 // https://developers.google.com/drive/v2/reference/apps/list 287 class AppList { 288 public: 289 AppList(); 290 ~AppList(); 291 292 // Registers the mapping between JSON field names and the members in this 293 // class. 294 static void RegisterJSONConverter( 295 base::JSONValueConverter<AppList>* converter); 296 297 // Creates app list from parsed JSON. 298 static scoped_ptr<AppList> CreateFrom(const base::Value& value); 299 300 // ETag for this resource. 301 const std::string& etag() const { return etag_; } 302 303 // Returns a vector of applications. 304 const ScopedVector<AppResource>& items() const { return items_; } 305 306 void set_etag(const std::string& etag) { 307 etag_ = etag; 308 } 309 void set_items(ScopedVector<AppResource> items) { 310 items_ = items.Pass(); 311 } 312 313 private: 314 friend class DriveAPIParserTest; 315 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, AppListParser); 316 317 // Parses and initializes data members from content of |value|. 318 // Return false if parsing fails. 319 bool Parse(const base::Value& value); 320 321 std::string etag_; 322 ScopedVector<AppResource> items_; 323 324 DISALLOW_COPY_AND_ASSIGN(AppList); 325 }; 326 327 // ParentReference represents a directory. 328 // https://developers.google.com/drive/v2/reference/parents 329 class ParentReference { 330 public: 331 ParentReference(); 332 ~ParentReference(); 333 334 // Registers the mapping between JSON field names and the members in this 335 // class. 336 static void RegisterJSONConverter( 337 base::JSONValueConverter<ParentReference>* converter); 338 339 // Creates parent reference from parsed JSON. 340 static scoped_ptr<ParentReference> CreateFrom(const base::Value& value); 341 342 // Returns the file id of the reference. 343 const std::string& file_id() const { return file_id_; } 344 345 // Returns the URL for the parent in Drive. 346 const GURL& parent_link() const { return parent_link_; } 347 348 // Returns true if the reference is root directory. 349 bool is_root() const { return is_root_; } 350 351 void set_file_id(const std::string& file_id) { file_id_ = file_id; } 352 void set_parent_link(const GURL& parent_link) { 353 parent_link_ = parent_link; 354 } 355 void set_is_root(bool is_root) { is_root_ = is_root; } 356 357 private: 358 friend class base::internal::RepeatedMessageConverter<ParentReference>; 359 360 // Parses and initializes data members from content of |value|. 361 // Return false if parsing fails. 362 bool Parse(const base::Value& value); 363 364 std::string file_id_; 365 GURL parent_link_; 366 bool is_root_; 367 368 DISALLOW_COPY_AND_ASSIGN(ParentReference); 369 }; 370 371 // FileLabels represents labels for file or folder. 372 // https://developers.google.com/drive/v2/reference/files 373 class FileLabels { 374 public: 375 FileLabels(); 376 ~FileLabels(); 377 378 // Registers the mapping between JSON field names and the members in this 379 // class. 380 static void RegisterJSONConverter( 381 base::JSONValueConverter<FileLabels>* converter); 382 383 // Creates about resource from parsed JSON. 384 static scoped_ptr<FileLabels> CreateFrom(const base::Value& value); 385 386 // Whether this file is starred by the user. 387 bool is_starred() const { return starred_; } 388 // Whether this file is hidden from the user. 389 bool is_hidden() const { return hidden_; } 390 // Whether this file has been trashed. 391 bool is_trashed() const { return trashed_; } 392 // Whether viewers are prevented from downloading this file. 393 bool is_restricted() const { return restricted_; } 394 // Whether this file has been viewed by this user. 395 bool is_viewed() const { return viewed_; } 396 397 void set_starred(bool starred) { starred_ = starred; } 398 void set_hidden(bool hidden) { hidden_ = hidden; } 399 void set_trashed(bool trashed) { trashed_ = trashed; } 400 void set_restricted(bool restricted) { restricted_ = restricted; } 401 void set_viewed(bool viewed) { viewed_ = viewed; } 402 403 private: 404 friend class FileResource; 405 406 // Parses and initializes data members from content of |value|. 407 // Return false if parsing fails. 408 bool Parse(const base::Value& value); 409 410 bool starred_; 411 bool hidden_; 412 bool trashed_; 413 bool restricted_; 414 bool viewed_; 415 416 DISALLOW_COPY_AND_ASSIGN(FileLabels); 417 }; 418 419 // ImageMediaMetadata represents image metadata for a file. 420 // https://developers.google.com/drive/v2/reference/files 421 class ImageMediaMetadata { 422 public: 423 ImageMediaMetadata(); 424 ~ImageMediaMetadata(); 425 426 // Registers the mapping between JSON field names and the members in this 427 // class. 428 static void RegisterJSONConverter( 429 base::JSONValueConverter<ImageMediaMetadata>* converter); 430 431 // Creates about resource from parsed JSON. 432 static scoped_ptr<ImageMediaMetadata> CreateFrom(const base::Value& value); 433 434 // Width of the image in pixels. 435 int width() const { return width_; } 436 // Height of the image in pixels. 437 int height() const { return height_; } 438 // Rotation of the image in clockwise degrees. 439 int rotation() const { return rotation_; } 440 441 void set_width(int width) { width_ = width; } 442 void set_height(int height) { height_ = height; } 443 void set_rotation(int rotation) { rotation_ = rotation; } 444 445 private: 446 friend class FileResource; 447 448 // Parses and initializes data members from content of |value|. 449 // Return false if parsing fails. 450 bool Parse(const base::Value& value); 451 452 int width_; 453 int height_; 454 int rotation_; 455 456 DISALLOW_COPY_AND_ASSIGN(ImageMediaMetadata); 457 }; 458 459 460 // FileResource represents a file or folder metadata in Drive. 461 // https://developers.google.com/drive/v2/reference/files 462 class FileResource { 463 public: 464 // Link to open a file resource on a web app with |app_id|. 465 struct OpenWithLink { 466 std::string app_id; 467 GURL open_url; 468 }; 469 470 FileResource(); 471 ~FileResource(); 472 473 // Registers the mapping between JSON field names and the members in this 474 // class. 475 static void RegisterJSONConverter( 476 base::JSONValueConverter<FileResource>* converter); 477 478 // Creates file resource from parsed JSON. 479 static scoped_ptr<FileResource> CreateFrom(const base::Value& value); 480 481 // Returns true if this is a directory. 482 // Note: "folder" is used elsewhere in this file to match Drive API reference, 483 // but outside this file we use "directory" to match HTML5 filesystem API. 484 bool IsDirectory() const; 485 486 // Returns file ID. This is unique in all files in Google Drive. 487 const std::string& file_id() const { return file_id_; } 488 489 // Returns ETag for this file. 490 const std::string& etag() const { return etag_; } 491 492 // Returns the link to JSON of this file itself. 493 const GURL& self_link() const { return self_link_; } 494 495 // Returns the title of this file. 496 const std::string& title() const { return title_; } 497 498 // Returns MIME type of this file. 499 const std::string& mime_type() const { return mime_type_; } 500 501 // Returns labels for this file. 502 const FileLabels& labels() const { return labels_; } 503 504 // Returns image media metadata for this file. 505 const ImageMediaMetadata& image_media_metadata() const { 506 return image_media_metadata_; 507 } 508 509 // Returns created time of this file. 510 const base::Time& created_date() const { return created_date_; } 511 512 // Returns modified time of this file. 513 const base::Time& modified_date() const { return modified_date_; } 514 515 // Returns modification time by the user. 516 const base::Time& modified_by_me_date() const { return modified_by_me_date_; } 517 518 // Returns last access time by the user. 519 const base::Time& last_viewed_by_me_date() const { 520 return last_viewed_by_me_date_; 521 } 522 523 // Returns time when the file was shared with the user. 524 const base::Time& shared_with_me_date() const { 525 return shared_with_me_date_; 526 } 527 528 // Returns the 'shared' attribute of the file. 529 bool shared() const { return shared_; } 530 531 // Returns the short-lived download URL for the file. This field exists 532 // only when the file content is stored in Drive. 533 const GURL& download_url() const { return download_url_; } 534 535 // Returns the extension part of the filename. 536 const std::string& file_extension() const { return file_extension_; } 537 538 // Returns MD5 checksum of this file. 539 const std::string& md5_checksum() const { return md5_checksum_; } 540 541 // Returns the size of this file in bytes. 542 int64 file_size() const { return file_size_; } 543 544 // Return the link to open the file in Google editor or viewer. 545 // E.g. Google Document, Google Spreadsheet. 546 const GURL& alternate_link() const { return alternate_link_; } 547 548 // Returns the link for embedding the file. 549 const GURL& embed_link() const { return embed_link_; } 550 551 // Returns parent references (directories) of this file. 552 const ScopedVector<ParentReference>& parents() const { return parents_; } 553 554 // Returns the link to the file's thumbnail. 555 const GURL& thumbnail_link() const { return thumbnail_link_; } 556 557 // Returns the link to open its downloadable content, using cookie based 558 // authentication. 559 const GURL& web_content_link() const { return web_content_link_; } 560 561 // Returns the list of links to open the resource with a web app. 562 const std::vector<OpenWithLink>& open_with_links() const { 563 return open_with_links_; 564 } 565 566 void set_file_id(const std::string& file_id) { 567 file_id_ = file_id; 568 } 569 void set_etag(const std::string& etag) { 570 etag_ = etag; 571 } 572 void set_self_link(const GURL& self_link) { 573 self_link_ = self_link; 574 } 575 void set_title(const std::string& title) { 576 title_ = title; 577 } 578 void set_mime_type(const std::string& mime_type) { 579 mime_type_ = mime_type; 580 } 581 FileLabels* mutable_labels() { 582 return &labels_; 583 } 584 ImageMediaMetadata* mutable_image_media_metadata() { 585 return &image_media_metadata_; 586 } 587 void set_created_date(const base::Time& created_date) { 588 created_date_ = created_date; 589 } 590 void set_modified_date(const base::Time& modified_date) { 591 modified_date_ = modified_date; 592 } 593 void set_modified_by_me_date(const base::Time& modified_by_me_date) { 594 modified_by_me_date_ = modified_by_me_date; 595 } 596 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) { 597 last_viewed_by_me_date_ = last_viewed_by_me_date; 598 } 599 void set_shared_with_me_date(const base::Time& shared_with_me_date) { 600 shared_with_me_date_ = shared_with_me_date; 601 } 602 void set_shared(bool shared) { 603 shared_ = shared; 604 } 605 void set_download_url(const GURL& download_url) { 606 download_url_ = download_url; 607 } 608 void set_file_extension(const std::string& file_extension) { 609 file_extension_ = file_extension; 610 } 611 void set_md5_checksum(const std::string& md5_checksum) { 612 md5_checksum_ = md5_checksum; 613 } 614 void set_file_size(int64 file_size) { 615 file_size_ = file_size; 616 } 617 void set_alternate_link(const GURL& alternate_link) { 618 alternate_link_ = alternate_link; 619 } 620 void set_embed_link(const GURL& embed_link) { 621 embed_link_ = embed_link; 622 } 623 void set_parents(ScopedVector<ParentReference> parents) { 624 parents_ = parents.Pass(); 625 } 626 void set_thumbnail_link(const GURL& thumbnail_link) { 627 thumbnail_link_ = thumbnail_link; 628 } 629 void set_web_content_link(const GURL& web_content_link) { 630 web_content_link_ = web_content_link; 631 } 632 633 private: 634 friend class base::internal::RepeatedMessageConverter<FileResource>; 635 friend class ChangeResource; 636 friend class FileList; 637 638 // Parses and initializes data members from content of |value|. 639 // Return false if parsing fails. 640 bool Parse(const base::Value& value); 641 642 std::string file_id_; 643 std::string etag_; 644 GURL self_link_; 645 std::string title_; 646 std::string mime_type_; 647 FileLabels labels_; 648 ImageMediaMetadata image_media_metadata_; 649 base::Time created_date_; 650 base::Time modified_date_; 651 base::Time modified_by_me_date_; 652 base::Time last_viewed_by_me_date_; 653 base::Time shared_with_me_date_; 654 bool shared_; 655 GURL download_url_; 656 std::string file_extension_; 657 std::string md5_checksum_; 658 int64 file_size_; 659 GURL alternate_link_; 660 GURL embed_link_; 661 ScopedVector<ParentReference> parents_; 662 GURL thumbnail_link_; 663 GURL web_content_link_; 664 std::vector<OpenWithLink> open_with_links_; 665 666 DISALLOW_COPY_AND_ASSIGN(FileResource); 667 }; 668 669 // FileList represents a collection of files and folders. 670 // https://developers.google.com/drive/v2/reference/files/list 671 class FileList { 672 public: 673 FileList(); 674 ~FileList(); 675 676 // Registers the mapping between JSON field names and the members in this 677 // class. 678 static void RegisterJSONConverter( 679 base::JSONValueConverter<FileList>* converter); 680 681 // Returns true if the |value| has kind field for FileList. 682 static bool HasFileListKind(const base::Value& value); 683 684 // Creates file list from parsed JSON. 685 static scoped_ptr<FileList> CreateFrom(const base::Value& value); 686 687 // Returns the ETag of the list. 688 const std::string& etag() const { return etag_; } 689 690 // Returns the page token for the next page of files, if the list is large 691 // to fit in one response. If this is empty, there is no more file lists. 692 const std::string& next_page_token() const { return next_page_token_; } 693 694 // Returns a link to the next page of files. The URL includes the next page 695 // token. 696 const GURL& next_link() const { return next_link_; } 697 698 // Returns a set of files in this list. 699 const ScopedVector<FileResource>& items() const { return items_; } 700 701 void set_etag(const std::string& etag) { 702 etag_ = etag; 703 } 704 void set_next_page_token(const std::string& next_page_token) { 705 next_page_token_ = next_page_token; 706 } 707 void set_next_link(const GURL& next_link) { 708 next_link_ = next_link; 709 } 710 void set_items(ScopedVector<FileResource> items) { 711 items_ = items.Pass(); 712 } 713 714 private: 715 friend class DriveAPIParserTest; 716 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, FileListParser); 717 718 // Parses and initializes data members from content of |value|. 719 // Return false if parsing fails. 720 bool Parse(const base::Value& value); 721 722 std::string etag_; 723 std::string next_page_token_; 724 GURL next_link_; 725 ScopedVector<FileResource> items_; 726 727 DISALLOW_COPY_AND_ASSIGN(FileList); 728 }; 729 730 // ChangeResource represents a change in a file. 731 // https://developers.google.com/drive/v2/reference/changes 732 class ChangeResource { 733 public: 734 ChangeResource(); 735 ~ChangeResource(); 736 737 // Registers the mapping between JSON field names and the members in this 738 // class. 739 static void RegisterJSONConverter( 740 base::JSONValueConverter<ChangeResource>* converter); 741 742 // Creates change resource from parsed JSON. 743 static scoped_ptr<ChangeResource> CreateFrom(const base::Value& value); 744 745 // Returns change ID for this change. This is a monotonically increasing 746 // number. 747 int64 change_id() const { return change_id_; } 748 749 // Returns a string file ID for corresponding file of the change. 750 const std::string& file_id() const { return file_id_; } 751 752 // Returns true if this file is deleted in the change. 753 bool is_deleted() const { return deleted_; } 754 755 // Returns FileResource of the file which the change refers to. 756 const FileResource* file() const { return file_.get(); } 757 758 void set_change_id(int64 change_id) { 759 change_id_ = change_id; 760 } 761 void set_file_id(const std::string& file_id) { 762 file_id_ = file_id; 763 } 764 void set_deleted(bool deleted) { 765 deleted_ = deleted; 766 } 767 void set_file(scoped_ptr<FileResource> file) { 768 file_ = file.Pass(); 769 } 770 771 private: 772 friend class base::internal::RepeatedMessageConverter<ChangeResource>; 773 friend class ChangeList; 774 775 // Parses and initializes data members from content of |value|. 776 // Return false if parsing fails. 777 bool Parse(const base::Value& value); 778 779 int64 change_id_; 780 std::string file_id_; 781 bool deleted_; 782 scoped_ptr<FileResource> file_; 783 784 DISALLOW_COPY_AND_ASSIGN(ChangeResource); 785 }; 786 787 // ChangeList represents a set of changes in the drive. 788 // https://developers.google.com/drive/v2/reference/changes/list 789 class ChangeList { 790 public: 791 ChangeList(); 792 ~ChangeList(); 793 794 // Registers the mapping between JSON field names and the members in this 795 // class. 796 static void RegisterJSONConverter( 797 base::JSONValueConverter<ChangeList>* converter); 798 799 // Returns true if the |value| has kind field for ChangeList. 800 static bool HasChangeListKind(const base::Value& value); 801 802 // Creates change list from parsed JSON. 803 static scoped_ptr<ChangeList> CreateFrom(const base::Value& value); 804 805 // Returns the ETag of the list. 806 const std::string& etag() const { return etag_; } 807 808 // Returns the page token for the next page of files, if the list is large 809 // to fit in one response. If this is empty, there is no more file lists. 810 const std::string& next_page_token() const { return next_page_token_; } 811 812 // Returns a link to the next page of files. The URL includes the next page 813 // token. 814 const GURL& next_link() const { return next_link_; } 815 816 // Returns the largest change ID number. 817 int64 largest_change_id() const { return largest_change_id_; } 818 819 // Returns a set of changes in this list. 820 const ScopedVector<ChangeResource>& items() const { return items_; } 821 822 void set_etag(const std::string& etag) { 823 etag_ = etag; 824 } 825 void set_next_page_token(const std::string& next_page_token) { 826 next_page_token_ = next_page_token; 827 } 828 void set_next_link(const GURL& next_link) { 829 next_link_ = next_link; 830 } 831 void set_largest_change_id(int64 largest_change_id) { 832 largest_change_id_ = largest_change_id; 833 } 834 void set_items(ScopedVector<ChangeResource> items) { 835 items_ = items.Pass(); 836 } 837 838 private: 839 friend class DriveAPIParserTest; 840 FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, ChangeListParser); 841 842 // Parses and initializes data members from content of |value|. 843 // Return false if parsing fails. 844 bool Parse(const base::Value& value); 845 846 std::string etag_; 847 std::string next_page_token_; 848 GURL next_link_; 849 int64 largest_change_id_; 850 ScopedVector<ChangeResource> items_; 851 852 DISALLOW_COPY_AND_ASSIGN(ChangeList); 853 }; 854 855 } // namespace google_apis 856 857 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_ 858