drive_api_parser.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_
  5. #define GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/compiler_specific.h"
  12. #include "base/gtest_prod_util.h"
  13. #include "base/strings/string_piece.h"
  14. #include "base/time/time.h"
  15. #include "url/gurl.h"
  16. namespace base {
  17. class Value;
  18. template <class StructType>
  19. class JSONValueConverter;
  20. namespace internal {
  21. template <class NestedType>
  22. class RepeatedMessageConverter;
  23. } // namespace internal
  24. } // namespace base
  25. namespace google_apis {
  26. // About resource represents the account information about the current user.
  27. // https://developers.google.com/drive/v2/reference/about
  28. class AboutResource {
  29. public:
  30. AboutResource();
  31. ~AboutResource();
  32. // Registers the mapping between JSON field names and the members in this
  33. // class.
  34. static void RegisterJSONConverter(
  35. base::JSONValueConverter<AboutResource>* converter);
  36. // Creates about resource from parsed JSON.
  37. static std::unique_ptr<AboutResource> CreateFrom(const base::Value& value);
  38. // Returns the largest change ID number.
  39. int64_t largest_change_id() const { return largest_change_id_; }
  40. // Returns total number of quota bytes.
  41. int64_t quota_bytes_total() const { return quota_bytes_total_; }
  42. // Returns the number of quota bytes used.
  43. int64_t quota_bytes_used_aggregate() const {
  44. return quota_bytes_used_aggregate_;
  45. }
  46. // Returns root folder ID.
  47. const std::string& root_folder_id() const { return root_folder_id_; }
  48. void set_largest_change_id(int64_t largest_change_id) {
  49. largest_change_id_ = largest_change_id;
  50. }
  51. void set_quota_bytes_total(int64_t quota_bytes_total) {
  52. quota_bytes_total_ = quota_bytes_total;
  53. }
  54. void set_quota_bytes_used_aggregate(int64_t quota_bytes_used_aggregate) {
  55. quota_bytes_used_aggregate_ = quota_bytes_used_aggregate;
  56. }
  57. void set_root_folder_id(const std::string& root_folder_id) {
  58. root_folder_id_ = root_folder_id;
  59. }
  60. private:
  61. friend class DriveAPIParserTest;
  62. FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, AboutResourceParser);
  63. // Parses and initializes data members from content of |value|.
  64. // Return false if parsing fails.
  65. bool Parse(const base::Value& value);
  66. int64_t largest_change_id_;
  67. int64_t quota_bytes_total_;
  68. int64_t quota_bytes_used_aggregate_;
  69. std::string root_folder_id_;
  70. // This class is copyable on purpose.
  71. };
  72. // Capabilities of a Team Drive indicate the permissions granted to the user
  73. // for the Team Drive and items within the Team Drive.
  74. // See "capabilities" in
  75. // https://developers.google.com/drive/v2/reference/teamdrives#resource.
  76. class TeamDriveCapabilities {
  77. public:
  78. TeamDriveCapabilities();
  79. TeamDriveCapabilities(const TeamDriveCapabilities& src);
  80. ~TeamDriveCapabilities();
  81. // Registers the mapping between JSON field names and the members in this
  82. // class.
  83. static void RegisterJSONConverter(
  84. base::JSONValueConverter<TeamDriveCapabilities>* converter);
  85. // Creates Team Drive resource from parsed JSON.
  86. static std::unique_ptr<TeamDriveCapabilities>
  87. CreateFrom(const base::Value& value);
  88. // Whether the current user can add children to folders in this Team Drive.
  89. bool can_add_children() const { return can_add_children_; }
  90. void set_can_add_children(bool can_add_children) {
  91. can_add_children_ = can_add_children;
  92. }
  93. // Whether the current user can comment on files in this Team Drive.
  94. bool can_comment() const { return can_comment_; }
  95. void set_can_comment(bool can_comment) { can_comment_ = can_comment; }
  96. // Whether files in this Team Drive can be copied by the current user.
  97. bool can_copy() const { return can_copy_; }
  98. void set_can_copy(bool can_copy) { can_copy_ = can_copy; }
  99. // Whether this Team Drive can be deleted by the current user.
  100. bool can_delete_team_drive() const { return can_delete_team_drive_; }
  101. void set_can_delete_team_drive(bool can_delete_team_drive) {
  102. can_delete_team_drive_ = can_delete_team_drive;
  103. }
  104. // Whether files in this Team Drive can be edited by the current user.
  105. bool can_download() const { return can_download_; }
  106. void set_can_download(bool can_download) { can_download_ = can_download; }
  107. // Whether files in this Team Drive can be edited by current user.
  108. bool can_edit() const { return can_edit_; }
  109. void set_can_edit(bool can_edit) { can_edit_ = can_edit; }
  110. // Whether the current user can list the children of folders in this Team
  111. // Drive.
  112. bool can_list_children() const { return can_list_children_; }
  113. void set_can_list_children(bool can_list_children) {
  114. can_list_children_ = can_list_children;
  115. }
  116. // Whether the current user can add members to this Team Drive or remove them
  117. // or change their role.
  118. bool can_manage_members() const { return can_manage_members_; }
  119. void set_can_manage_members(bool can_manage_members) {
  120. can_manage_members_ = can_manage_members;
  121. }
  122. // Whether the current user has read access to the Revisions resource of files
  123. // in this Team Drive.
  124. bool can_read_revisions() const { return can_read_revisions_; }
  125. void set_can_read_revisions(bool can_read_revisions) {
  126. can_read_revisions_ = can_read_revisions;
  127. }
  128. // Whether the current user can remove children from folders in this Team
  129. // Drive.
  130. bool can_remove_children() const { return can_remove_children_; }
  131. void set_can_remove_children(bool can_remove_children) {
  132. can_remove_children_ = can_remove_children;
  133. }
  134. // Whether files or folders in this Team Drive can be renamed by the current
  135. // user.
  136. bool can_rename() const { return can_rename_; }
  137. void set_can_rename(bool can_rename) { can_rename_ = can_rename; }
  138. // Whether this Team Drive can be renamed by the current user.
  139. bool can_rename_team_drive() const { return can_rename_team_drive_; }
  140. void set_can_rename_team_drive(bool can_rename_team_drive) {
  141. can_rename_team_drive_ = can_rename_team_drive;
  142. }
  143. // Whether files or folders in this Team Drive can be shared by the current
  144. // user.
  145. bool can_share() const { return can_share_; }
  146. void set_can_share(bool can_share) { can_share_ = can_share; }
  147. private:
  148. bool can_add_children_;
  149. bool can_comment_;
  150. bool can_copy_;
  151. bool can_delete_team_drive_;
  152. bool can_download_;
  153. bool can_edit_;
  154. bool can_list_children_;
  155. bool can_manage_members_;
  156. bool can_read_revisions_;
  157. bool can_remove_children_;
  158. bool can_rename_;
  159. bool can_rename_team_drive_;
  160. bool can_share_;
  161. };
  162. // Team Drive resource represents the metadata about Team Drive itself, such as
  163. // the name.
  164. class TeamDriveResource {
  165. public:
  166. TeamDriveResource();
  167. ~TeamDriveResource();
  168. // Registers the mapping between JSON field names and the members in this
  169. // class.
  170. static void RegisterJSONConverter(
  171. base::JSONValueConverter<TeamDriveResource>* converter);
  172. // Creates Team Drive resource from parsed JSON.
  173. static std::unique_ptr<TeamDriveResource>
  174. CreateFrom(const base::Value& value);
  175. // The ID of this Team Drive. The ID is the same as the top-level folder for
  176. // this Team Drive.
  177. const std::string& id() const { return id_; }
  178. void set_id(const std::string& id) { id_ = id; }
  179. // The name of this Team Drive.
  180. const std::string& name() const { return name_; }
  181. void set_name(const std::string& name) { name_ = name; }
  182. // Capabilities the current user has on this Team Drive.
  183. const TeamDriveCapabilities& capabilities() const { return capabilities_; }
  184. void set_capabilities(const TeamDriveCapabilities& capabilities) {
  185. capabilities_ = capabilities;
  186. }
  187. private:
  188. friend class DriveAPIParserTest;
  189. FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, TeamDriveResourceParser);
  190. // Parses and initializes data members from content of |value|.
  191. // Return false if parsing fails.
  192. bool Parse(const base::Value& value);
  193. std::string id_;
  194. std::string name_;
  195. TeamDriveCapabilities capabilities_;
  196. };
  197. // TeamDriveList represents a collection of Team Drives.
  198. // https://developers.google.com/drive/v2/reference/teamdrives/list
  199. class TeamDriveList {
  200. public:
  201. TeamDriveList();
  202. TeamDriveList(const TeamDriveList&) = delete;
  203. TeamDriveList& operator=(const TeamDriveList&) = delete;
  204. ~TeamDriveList();
  205. // Registers the mapping between JSON field names and the members in this
  206. // class.
  207. static void RegisterJSONConverter(
  208. base::JSONValueConverter<TeamDriveList>* converter);
  209. // Returns true if the |value| has kind field for TeamDriveList.
  210. static bool HasTeamDriveListKind(const base::Value& value);
  211. // Creates file list from parsed JSON.
  212. static std::unique_ptr<TeamDriveList> CreateFrom(const base::Value& value);
  213. // Returns a page token for the next page of Team Drives.
  214. const std::string& next_page_token() const { return next_page_token_; }
  215. void set_next_page_token(const std::string& next_page_token) {
  216. this->next_page_token_ = next_page_token;
  217. }
  218. // Returns a set of Team Drives in this list.
  219. const std::vector<std::unique_ptr<TeamDriveResource>>& items() const {
  220. return items_;
  221. }
  222. std::vector<std::unique_ptr<TeamDriveResource>>* mutable_items() {
  223. return &items_;
  224. }
  225. private:
  226. friend class DriveAPIParserTest;
  227. FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, TeamDriveListParser);
  228. // Parses and initializes data members from content of |value|.
  229. // Return false if parsing fails.
  230. bool Parse(const base::Value& value);
  231. std::string next_page_token_;
  232. std::vector<std::unique_ptr<TeamDriveResource>> items_;
  233. };
  234. // ParentReference represents a directory.
  235. // https://developers.google.com/drive/v2/reference/parents
  236. class ParentReference {
  237. public:
  238. ParentReference();
  239. ~ParentReference();
  240. // Registers the mapping between JSON field names and the members in this
  241. // class.
  242. static void RegisterJSONConverter(
  243. base::JSONValueConverter<ParentReference>* converter);
  244. // Creates parent reference from parsed JSON.
  245. static std::unique_ptr<ParentReference> CreateFrom(const base::Value& value);
  246. // Returns the file id of the reference.
  247. const std::string& file_id() const { return file_id_; }
  248. void set_file_id(const std::string& file_id) { file_id_ = file_id; }
  249. private:
  250. // Parses and initializes data members from content of |value|.
  251. // Return false if parsing fails.
  252. bool Parse(const base::Value& value);
  253. std::string file_id_;
  254. };
  255. // FileLabels represents labels for file or folder.
  256. // https://developers.google.com/drive/v2/reference/files
  257. class FileLabels {
  258. public:
  259. FileLabels();
  260. ~FileLabels();
  261. // Registers the mapping between JSON field names and the members in this
  262. // class.
  263. static void RegisterJSONConverter(
  264. base::JSONValueConverter<FileLabels>* converter);
  265. // Creates about resource from parsed JSON.
  266. static std::unique_ptr<FileLabels> CreateFrom(const base::Value& value);
  267. // Whether this file has been trashed.
  268. bool is_trashed() const { return trashed_; }
  269. // Whether this file is starred by the user.
  270. bool is_starred() const { return starred_; }
  271. void set_trashed(bool trashed) { trashed_ = trashed; }
  272. void set_starred(bool starred) { starred_ = starred; }
  273. private:
  274. friend class FileResource;
  275. // Parses and initializes data members from content of |value|.
  276. // Return false if parsing fails.
  277. bool Parse(const base::Value& value);
  278. bool trashed_;
  279. bool starred_;
  280. };
  281. // ImageMediaMetadata represents image metadata for a file.
  282. // https://developers.google.com/drive/v2/reference/files
  283. class ImageMediaMetadata {
  284. public:
  285. ImageMediaMetadata();
  286. ~ImageMediaMetadata();
  287. // Registers the mapping between JSON field names and the members in this
  288. // class.
  289. static void RegisterJSONConverter(
  290. base::JSONValueConverter<ImageMediaMetadata>* converter);
  291. // Creates about resource from parsed JSON.
  292. static std::unique_ptr<ImageMediaMetadata> CreateFrom(
  293. const base::Value& value);
  294. // Width of the image in pixels.
  295. int width() const { return width_; }
  296. // Height of the image in pixels.
  297. int height() const { return height_; }
  298. // Rotation of the image in clockwise degrees.
  299. int rotation() const { return rotation_; }
  300. void set_width(int width) { width_ = width; }
  301. void set_height(int height) { height_ = height; }
  302. void set_rotation(int rotation) { rotation_ = rotation; }
  303. private:
  304. friend class FileResource;
  305. // Parses and initializes data members from content of |value|.
  306. // Return false if parsing fails.
  307. bool Parse(const base::Value& value);
  308. int width_;
  309. int height_;
  310. int rotation_;
  311. };
  312. // Capabilities of a file resource indicate the permissions granted to the user
  313. // for the file (or items within the folder).
  314. // See "capabilities" in
  315. // https://developers.google.com/drive/v2/reference/files#resource.
  316. class FileResourceCapabilities {
  317. public:
  318. FileResourceCapabilities();
  319. FileResourceCapabilities(const FileResourceCapabilities& src);
  320. ~FileResourceCapabilities();
  321. // Registers the mapping between JSON field names and the members in this
  322. // class.
  323. static void RegisterJSONConverter(
  324. base::JSONValueConverter<FileResourceCapabilities>* converter);
  325. // Creates a FileResourceCapabilities from parsed JSON.
  326. static std::unique_ptr<FileResourceCapabilities> CreateFrom(
  327. const base::Value& value);
  328. // Whether the current user can add children to this folder. This is always
  329. // false when the item is not a folder.
  330. bool can_add_children() const { return can_add_children_; }
  331. void set_can_add_children(bool can_add_children) {
  332. can_add_children_ = can_add_children;
  333. }
  334. // Whether the current user can change the restricted download label of this
  335. // file.
  336. bool can_change_restricted_download() const {
  337. return can_change_restricted_download_;
  338. }
  339. void set_can_change_restricted_download(bool can_change_restricted_download) {
  340. can_change_restricted_download_ = can_change_restricted_download;
  341. }
  342. // Whether the current user can comment on this file.
  343. bool can_comment() const { return can_comment_; }
  344. void set_can_comment(bool can_comment) { can_comment_ = can_comment; }
  345. // Whether the current user can copy this file. For a Team Drive item, whether
  346. // the current user can copy non-folder descendants of this item, or this item
  347. // itself if it is not a folder.
  348. bool can_copy() const { return can_copy_; }
  349. void set_can_copy(bool can_copy) { can_copy_ = can_copy; }
  350. // Whether the current user can delete this file.
  351. bool can_delete() const { return can_delete_; }
  352. void set_can_delete(bool can_delete) { can_delete_ = can_delete; }
  353. // Whether the current user can download this file.
  354. bool can_download() const { return can_download_; }
  355. void set_can_download(bool can_download) { can_download_ = can_download; }
  356. // Whether the current user can edit this file.
  357. bool can_edit() const { return can_edit_; }
  358. void set_can_edit(bool can_edit) { can_edit_ = can_edit; }
  359. // Whether the current user can list the children of this folder. This is
  360. // always false when the item is not a folder.
  361. bool can_list_children() const { return can_list_children_; }
  362. void set_can_list_children(bool can_list_children) {
  363. can_list_children_ = can_list_children;
  364. }
  365. // Whether the current user can move this item into a Team Drive. If the item
  366. // is in a Team Drive, this field is equivalent to canMoveTeamDriveItem.
  367. bool can_move_item_into_team_drive() const {
  368. return can_move_item_into_team_drive_;
  369. }
  370. void set_can_move_item_into_team_drive(bool can_move_item_into_team_drive) {
  371. can_move_item_into_team_drive_ = can_move_item_into_team_drive;
  372. }
  373. // Whether the current user can move this Team Drive item by changing its
  374. // parent. Note that a request to change the parent for this item may still
  375. // fail depending on the new parent that is being added. Only populated for
  376. // Team Drive files.
  377. bool can_move_team_drive_item() const { return can_move_team_drive_item_; }
  378. void set_can_move_team_drive_item(bool can_move_team_drive_item) {
  379. can_move_team_drive_item_ = can_move_team_drive_item;
  380. }
  381. // Whether the current user can read the revisions resource of this file. For
  382. // a Team Drive item, whether revisions of non-folder descendants of this
  383. // item, or this item itself if it is not a folder, can be read.
  384. bool can_read_revisions() const { return can_read_revisions_; }
  385. void set_can_read_revisions(bool can_read_revisions) {
  386. can_read_revisions_ = can_read_revisions;
  387. }
  388. // Whether the current user can read the Team Drive to which this file
  389. // belongs. Only populated for Team Drive files.
  390. bool can_read_team_drive() const { return can_read_team_drive_; }
  391. void set_can_read_team_drive(bool can_read_team_drive) {
  392. can_read_team_drive_ = can_read_team_drive;
  393. }
  394. // Whether the current user can remove children from this folder. This is
  395. // always false when the item is not a folder.
  396. bool can_remove_children() const { return can_remove_children_; }
  397. void set_can_remove_children(bool can_remove_children) {
  398. can_remove_children_ = can_remove_children;
  399. }
  400. // Whether the current user can rename this file.
  401. bool can_rename() const { return can_rename_; }
  402. void set_can_rename(bool can_rename) { can_rename_ = can_rename; }
  403. // Whether the current user can modify the sharing settings for this file.
  404. bool can_share() const { return can_share_; }
  405. void set_can_share(bool can_share) { can_share_ = can_share; }
  406. // Whether the current user can move this file to trash.
  407. bool can_trash() const { return can_trash_; }
  408. void set_can_trash(bool can_trash) { can_trash_ = can_trash; }
  409. // Whether the current user can restore this file from trash.
  410. bool can_untrash() const { return can_untrash_; }
  411. void set_can_untrash(bool can_untrash) { can_untrash_ = can_untrash; }
  412. private:
  413. bool can_add_children_;
  414. bool can_change_restricted_download_;
  415. bool can_comment_;
  416. bool can_copy_;
  417. bool can_delete_;
  418. bool can_download_;
  419. bool can_edit_;
  420. bool can_list_children_;
  421. bool can_move_item_into_team_drive_;
  422. bool can_move_team_drive_item_;
  423. bool can_read_revisions_;
  424. bool can_read_team_drive_;
  425. bool can_remove_children_;
  426. bool can_rename_;
  427. bool can_share_;
  428. bool can_trash_;
  429. bool can_untrash_;
  430. };
  431. // FileResource represents a file or folder metadata in Drive.
  432. // https://developers.google.com/drive/v2/reference/files
  433. class FileResource {
  434. public:
  435. // Link to open a file resource on a web app with |app_id|.
  436. struct OpenWithLink {
  437. std::string app_id;
  438. GURL open_url;
  439. };
  440. FileResource();
  441. FileResource(const FileResource& other);
  442. ~FileResource();
  443. // Registers the mapping between JSON field names and the members in this
  444. // class.
  445. static void RegisterJSONConverter(
  446. base::JSONValueConverter<FileResource>* converter);
  447. // Creates file resource from parsed JSON.
  448. static std::unique_ptr<FileResource> CreateFrom(const base::Value& value);
  449. // Returns true if this is a directory.
  450. // Note: "folder" is used elsewhere in this file to match Drive API reference,
  451. // but outside this file we use "directory" to match HTML5 filesystem API.
  452. bool IsDirectory() const;
  453. // Returns true if this is a hosted document.
  454. // A hosted document is a document in one of Google Docs formats (Documents,
  455. // Spreadsheets, Slides, ...) whose content is not exposed via the API. It is
  456. // available only as |alternate_link()| to the document hosted on the server.
  457. bool IsHostedDocument() const;
  458. // Returns file ID. This is unique in all files in Google Drive.
  459. const std::string& file_id() const { return file_id_; }
  460. // Returns ETag for this file.
  461. const std::string& etag() const { return etag_; }
  462. // Returns the title of this file.
  463. const std::string& title() const { return title_; }
  464. // Returns MIME type of this file.
  465. const std::string& mime_type() const { return mime_type_; }
  466. // Returns labels for this file.
  467. const FileLabels& labels() const { return labels_; }
  468. // Returns image media metadata for this file.
  469. const ImageMediaMetadata& image_media_metadata() const {
  470. return image_media_metadata_;
  471. }
  472. // Returns created time of this file.
  473. const base::Time& created_date() const { return created_date_; }
  474. // Returns modified time of this file.
  475. const base::Time& modified_date() const { return modified_date_; }
  476. // Returns last modified time by the user.
  477. const base::Time& modified_by_me_date() const { return modified_by_me_date_; }
  478. // Returns last access time by the user.
  479. const base::Time& last_viewed_by_me_date() const {
  480. return last_viewed_by_me_date_;
  481. }
  482. // Returns time when the file was shared with the user.
  483. const base::Time& shared_with_me_date() const {
  484. return shared_with_me_date_;
  485. }
  486. // Returns the 'shared' attribute of the file.
  487. bool shared() const { return shared_; }
  488. // Returns MD5 checksum of this file.
  489. const std::string& md5_checksum() const { return md5_checksum_; }
  490. // Returns the size of this file in bytes.
  491. int64_t file_size() const { return file_size_; }
  492. // Return the link to open the file in Google editor or viewer.
  493. // E.g. Google Document, Google Spreadsheet.
  494. const GURL& alternate_link() const { return alternate_link_; }
  495. // Returns URL to the share dialog UI.
  496. const GURL& share_link() const { return share_link_; }
  497. // Returns parent references (directories) of this file.
  498. const std::vector<ParentReference>& parents() const { return parents_; }
  499. // Returns the list of links to open the resource with a web app.
  500. const std::vector<OpenWithLink>& open_with_links() const {
  501. return open_with_links_;
  502. }
  503. void set_file_id(const std::string& file_id) {
  504. file_id_ = file_id;
  505. }
  506. void set_etag(const std::string& etag) {
  507. etag_ = etag;
  508. }
  509. void set_title(const std::string& title) {
  510. title_ = title;
  511. }
  512. void set_mime_type(const std::string& mime_type) {
  513. mime_type_ = mime_type;
  514. }
  515. FileLabels* mutable_labels() {
  516. return &labels_;
  517. }
  518. ImageMediaMetadata* mutable_image_media_metadata() {
  519. return &image_media_metadata_;
  520. }
  521. void set_created_date(const base::Time& created_date) {
  522. created_date_ = created_date;
  523. }
  524. void set_modified_date(const base::Time& modified_date) {
  525. modified_date_ = modified_date;
  526. }
  527. void set_modified_by_me_date(const base::Time& modified_by_me_date) {
  528. modified_by_me_date_ = modified_by_me_date;
  529. }
  530. void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
  531. last_viewed_by_me_date_ = last_viewed_by_me_date;
  532. }
  533. void set_shared_with_me_date(const base::Time& shared_with_me_date) {
  534. shared_with_me_date_ = shared_with_me_date;
  535. }
  536. void set_shared(bool shared) {
  537. shared_ = shared;
  538. }
  539. void set_md5_checksum(const std::string& md5_checksum) {
  540. md5_checksum_ = md5_checksum;
  541. }
  542. void set_file_size(int64_t file_size) { file_size_ = file_size; }
  543. void set_alternate_link(const GURL& alternate_link) {
  544. alternate_link_ = alternate_link;
  545. }
  546. void set_share_link(const GURL& share_link) {
  547. share_link_ = share_link;
  548. }
  549. std::vector<ParentReference>* mutable_parents() { return &parents_; }
  550. std::vector<OpenWithLink>* mutable_open_with_links() {
  551. return &open_with_links_;
  552. }
  553. // Capabilities the current user has on this file resource.
  554. const FileResourceCapabilities& capabilities() const { return capabilities_; }
  555. void set_capabilities(const FileResourceCapabilities& capabilities) {
  556. capabilities_ = capabilities;
  557. }
  558. // ID of the Team Drive the file resides in. Will be empty if the file
  559. // is not in a team drive.
  560. const std::string& team_drive_id() const { return team_drive_id_; }
  561. void set_team_drive_id(const std::string& team_drive_id) {
  562. team_drive_id_ = team_drive_id;
  563. }
  564. private:
  565. friend class base::internal::RepeatedMessageConverter<FileResource>;
  566. friend class ChangeResource;
  567. friend class FileList;
  568. // Parses and initializes data members from content of |value|.
  569. // Return false if parsing fails.
  570. bool Parse(const base::Value& value);
  571. std::string file_id_;
  572. std::string etag_;
  573. std::string title_;
  574. std::string mime_type_;
  575. FileLabels labels_;
  576. ImageMediaMetadata image_media_metadata_;
  577. base::Time created_date_;
  578. base::Time modified_date_;
  579. base::Time modified_by_me_date_;
  580. base::Time last_viewed_by_me_date_;
  581. base::Time shared_with_me_date_;
  582. bool shared_;
  583. std::string md5_checksum_;
  584. int64_t file_size_;
  585. GURL alternate_link_;
  586. GURL share_link_;
  587. std::vector<ParentReference> parents_;
  588. std::vector<OpenWithLink> open_with_links_;
  589. FileResourceCapabilities capabilities_;
  590. std::string team_drive_id_;
  591. };
  592. // FileList represents a collection of files and folders.
  593. // https://developers.google.com/drive/v2/reference/files/list
  594. class FileList {
  595. public:
  596. FileList();
  597. FileList(const FileList&) = delete;
  598. FileList& operator=(const FileList&) = delete;
  599. ~FileList();
  600. // Registers the mapping between JSON field names and the members in this
  601. // class.
  602. static void RegisterJSONConverter(
  603. base::JSONValueConverter<FileList>* converter);
  604. // Returns true if the |value| has kind field for FileList.
  605. static bool HasFileListKind(const base::Value& value);
  606. // Creates file list from parsed JSON.
  607. static std::unique_ptr<FileList> CreateFrom(const base::Value& value);
  608. // Returns a link to the next page of files. The URL includes the next page
  609. // token.
  610. const GURL& next_link() const { return next_link_; }
  611. // Returns a set of files in this list.
  612. const std::vector<std::unique_ptr<FileResource>>& items() const {
  613. return items_;
  614. }
  615. std::vector<std::unique_ptr<FileResource>>* mutable_items() {
  616. return &items_;
  617. }
  618. void set_next_link(const GURL& next_link) {
  619. next_link_ = next_link;
  620. }
  621. private:
  622. friend class DriveAPIParserTest;
  623. FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, FileListParser);
  624. // Parses and initializes data members from content of |value|.
  625. // Return false if parsing fails.
  626. bool Parse(const base::Value& value);
  627. GURL next_link_;
  628. std::vector<std::unique_ptr<FileResource>> items_;
  629. };
  630. // ChangeResource represents a change in a file.
  631. // https://developers.google.com/drive/v2/reference/changes
  632. class ChangeResource {
  633. public:
  634. enum ChangeType {
  635. UNKNOWN, // Uninitialized state.
  636. FILE,
  637. TEAM_DRIVE,
  638. };
  639. ChangeResource();
  640. ChangeResource(const ChangeResource&) = delete;
  641. ChangeResource& operator=(const ChangeResource&) = delete;
  642. ~ChangeResource();
  643. // Registers the mapping between JSON field names and the members in this
  644. // class.
  645. static void RegisterJSONConverter(
  646. base::JSONValueConverter<ChangeResource>* converter);
  647. // Creates change resource from parsed JSON.
  648. static std::unique_ptr<ChangeResource> CreateFrom(const base::Value& value);
  649. // Returns change ID for this change. This is a monotonically increasing
  650. // number.
  651. int64_t change_id() const { return change_id_; }
  652. // Returns whether this is a change of a file or a team drive.
  653. ChangeType type() const { return type_; }
  654. // Returns a string file ID for corresponding file of the change.
  655. // Valid only when type == FILE.
  656. const std::string& file_id() const {
  657. DCHECK_EQ(FILE, type_);
  658. return file_id_;
  659. }
  660. // Returns true if this file is deleted in the change.
  661. bool is_deleted() const { return deleted_; }
  662. // Returns FileResource of the file which the change refers to.
  663. // Valid only when type == FILE.
  664. const FileResource* file() const {
  665. DCHECK_EQ(FILE, type_);
  666. return file_.get();
  667. }
  668. FileResource* mutable_file() {
  669. DCHECK_EQ(FILE, type_);
  670. return file_.get();
  671. }
  672. // Returns TeamDriveResource which the change refers to.
  673. // Valid only when type == TEAM_DRIVE.
  674. const TeamDriveResource* team_drive() const {
  675. DCHECK_EQ(TEAM_DRIVE, type_);
  676. return team_drive_.get();
  677. }
  678. TeamDriveResource* mutable_team_drive() {
  679. DCHECK_EQ(TEAM_DRIVE, type_);
  680. return team_drive_.get();
  681. }
  682. // Returns the ID of the Team Drive. Valid only when type == TEAM_DRIVE.
  683. const std::string& team_drive_id() const {
  684. DCHECK_EQ(TEAM_DRIVE, type_);
  685. return team_drive_id_;
  686. }
  687. // Returns the time of this modification.
  688. const base::Time& modification_date() const { return modification_date_; }
  689. void set_change_id(int64_t change_id) { change_id_ = change_id; }
  690. void set_type(ChangeType type) { type_ = type; }
  691. void set_file_id(const std::string& file_id) {
  692. file_id_ = file_id;
  693. }
  694. void set_deleted(bool deleted) {
  695. deleted_ = deleted;
  696. }
  697. void set_file(std::unique_ptr<FileResource> file) { file_ = std::move(file); }
  698. void set_team_drive(std::unique_ptr<TeamDriveResource> team_drive) {
  699. team_drive_ = std::move(team_drive);
  700. }
  701. void set_team_drive_id(const std::string& team_drive_id) {
  702. team_drive_id_ = team_drive_id;
  703. }
  704. void set_modification_date(const base::Time& modification_date) {
  705. modification_date_ = modification_date;
  706. }
  707. private:
  708. friend class base::internal::RepeatedMessageConverter<ChangeResource>;
  709. friend class ChangeList;
  710. // Parses and initializes data members from content of |value|.
  711. // Return false if parsing fails.
  712. bool Parse(const base::Value& value);
  713. // Extracts the change type from the given string. Returns false and does
  714. // not change |result| when |type_name| has an unrecognizable value.
  715. static bool GetType(base::StringPiece type_name,
  716. ChangeResource::ChangeType* result);
  717. int64_t change_id_;
  718. ChangeType type_;
  719. std::string file_id_;
  720. bool deleted_;
  721. std::unique_ptr<FileResource> file_;
  722. base::Time modification_date_;
  723. std::string team_drive_id_;
  724. std::unique_ptr<TeamDriveResource> team_drive_;
  725. };
  726. // ChangeList represents a set of changes in the drive.
  727. // https://developers.google.com/drive/v2/reference/changes/list
  728. class ChangeList {
  729. public:
  730. ChangeList();
  731. ChangeList(const ChangeList&) = delete;
  732. ChangeList& operator=(const ChangeList&) = delete;
  733. ~ChangeList();
  734. // Registers the mapping between JSON field names and the members in this
  735. // class.
  736. static void RegisterJSONConverter(
  737. base::JSONValueConverter<ChangeList>* converter);
  738. // Returns true if the |value| has kind field for ChangeList.
  739. static bool HasChangeListKind(const base::Value& value);
  740. // Creates change list from parsed JSON.
  741. static std::unique_ptr<ChangeList> CreateFrom(const base::Value& value);
  742. // Returns a link to the next page of files. The URL includes the next page
  743. // token.
  744. const GURL& next_link() const { return next_link_; }
  745. // Returns the largest change ID number.
  746. int64_t largest_change_id() const { return largest_change_id_; }
  747. // Returns the new start page token, only if the end of current change list
  748. // was reached.
  749. const std::string& new_start_page_token() const {
  750. return new_start_page_token_;
  751. }
  752. // Returns a set of changes in this list.
  753. const std::vector<std::unique_ptr<ChangeResource>>& items() const {
  754. return items_;
  755. }
  756. std::vector<std::unique_ptr<ChangeResource>>* mutable_items() {
  757. return &items_;
  758. }
  759. void set_next_link(const GURL& next_link) {
  760. next_link_ = next_link;
  761. }
  762. void set_largest_change_id(int64_t largest_change_id) {
  763. largest_change_id_ = largest_change_id;
  764. }
  765. void set_new_start_page_token(const std::string& new_start_page_token) {
  766. new_start_page_token_ = new_start_page_token;
  767. }
  768. private:
  769. friend class DriveAPIParserTest;
  770. FRIEND_TEST_ALL_PREFIXES(DriveAPIParserTest, ChangeListParser);
  771. // Parses and initializes data members from content of |value|.
  772. // Return false if parsing fails.
  773. bool Parse(const base::Value& value);
  774. GURL next_link_;
  775. int64_t largest_change_id_;
  776. std::string new_start_page_token_;
  777. std::vector<std::unique_ptr<ChangeResource>> items_;
  778. };
  779. // StartPageToken represets the starting pageToken for listing changes in the
  780. // users corpus or in a team drive.
  781. // https://developers.google.com/drive/v2/reference/changes/getStartPageToken
  782. class StartPageToken {
  783. public:
  784. StartPageToken();
  785. ~StartPageToken();
  786. // Registers the mapping between JSON field names and the members in this
  787. // class.
  788. static void RegisterJSONConverter(
  789. base::JSONValueConverter<StartPageToken>* converter);
  790. // Creates StartPageToken from parsed JSON
  791. static std::unique_ptr<StartPageToken> CreateFrom(const base::Value& value);
  792. const std::string& start_page_token() const { return start_page_token_; }
  793. void set_start_page_token(const std::string& token) {
  794. start_page_token_ = token;
  795. }
  796. private:
  797. // Pareses and initializes data members from content of |value|.
  798. // Returns false if parsing fails.
  799. bool Parse(const base::Value& value);
  800. std::string start_page_token_;
  801. };
  802. } // namespace google_apis
  803. #endif // GOOGLE_APIS_DRIVE_DRIVE_API_PARSER_H_