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