Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / google_apis / drive / drive_api_requests.h
blob702414477d89feaf57e3bc2ae6ba2062bee2f87d
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/location.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/task_runner_util.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "google_apis/drive/base_requests.h"
19 #include "google_apis/drive/drive_api_parser.h"
20 #include "google_apis/drive/drive_api_url_generator.h"
21 #include "google_apis/drive/drive_common_callbacks.h"
23 namespace google_apis {
25 // Callback used for requests that the server returns FileList data
26 // formatted into JSON value.
27 typedef base::Callback<void(DriveApiErrorCode error,
28 scoped_ptr<FileList> entry)> FileListCallback;
30 // Callback used for requests that the server returns ChangeList data
31 // formatted into JSON value.
32 typedef base::Callback<void(DriveApiErrorCode error,
33 scoped_ptr<ChangeList> entry)> ChangeListCallback;
35 namespace drive {
37 // Represents a property for a file or a directory.
38 // https://developers.google.com/drive/v2/reference/properties
39 class Property {
40 public:
41 Property();
42 ~Property();
44 // Visibility of the property. Either limited to the same client, or to any.
45 enum Visibility { VISIBILITY_PRIVATE, VISIBILITY_PUBLIC };
47 // Whether the property is public or private.
48 Visibility visibility() const { return visibility_; }
50 // Name of the property.
51 const std::string& key() const { return key_; }
53 // Value of the property.
54 const std::string& value() const { return value_; }
56 void set_visibility(Visibility visibility) { visibility_ = visibility; }
57 void set_key(const std::string& key) { key_ = key; }
58 void set_value(const std::string& value) { value_ = value; }
60 private:
61 Visibility visibility_;
62 std::string key_;
63 std::string value_;
66 // List of properties for a single file or a directory.
67 typedef std::vector<Property> Properties;
69 // Child response embedded in multipart parent response.
70 struct MultipartHttpResponse {
71 MultipartHttpResponse();
72 ~MultipartHttpResponse();
73 DriveApiErrorCode code;
74 std::string body;
77 // Splits multipart |response| into |parts|. Each part must be HTTP sub-response
78 // of drive batch request. |content_type| is a value of Content-Type response
79 // header. Returns true on succcess.
80 bool ParseMultipartResponse(const std::string& content_type,
81 const std::string& response,
82 std::vector<MultipartHttpResponse>* parts);
84 //============================ DriveApiPartialFieldRequest ====================
86 // This is base class of the Drive API related requests. All Drive API requests
87 // support partial request (to improve the performance). The function can be
88 // shared among the Drive API requests.
89 // See also https://developers.google.com/drive/performance
90 class DriveApiPartialFieldRequest : public UrlFetchRequestBase {
91 public:
92 explicit DriveApiPartialFieldRequest(RequestSender* sender);
93 ~DriveApiPartialFieldRequest() override;
95 // Optional parameter.
96 const std::string& fields() const { return fields_; }
97 void set_fields(const std::string& fields) { fields_ = fields; }
99 protected:
100 // UrlFetchRequestBase overrides.
101 GURL GetURL() const override;
103 // Derived classes should override GetURLInternal instead of GetURL()
104 // directly.
105 virtual GURL GetURLInternal() const = 0;
107 private:
108 std::string fields_;
110 DISALLOW_COPY_AND_ASSIGN(DriveApiPartialFieldRequest);
113 //============================ DriveApiDataRequest ===========================
115 // The base class of Drive API related requests that receive a JSON response
116 // representing |DataType|.
117 template<class DataType>
118 class DriveApiDataRequest : public DriveApiPartialFieldRequest {
119 public:
120 typedef base::Callback<void(DriveApiErrorCode error,
121 scoped_ptr<DataType> data)> Callback;
123 // |callback| is called when the request finishes either by success or by
124 // failure. On success, a JSON Value object is passed. It must not be null.
125 DriveApiDataRequest(RequestSender* sender, const Callback& callback)
126 : DriveApiPartialFieldRequest(sender),
127 callback_(callback),
128 weak_ptr_factory_(this) {
129 DCHECK(!callback_.is_null());
131 ~DriveApiDataRequest() override {}
133 protected:
134 // UrlFetchRequestBase overrides.
135 void ProcessURLFetchResults(const net::URLFetcher* source) override {
136 DriveApiErrorCode error = GetErrorCode();
137 switch (error) {
138 case HTTP_SUCCESS:
139 case HTTP_CREATED:
140 base::PostTaskAndReplyWithResult(
141 blocking_task_runner(),
142 FROM_HERE,
143 base::Bind(&DriveApiDataRequest::Parse, response_writer()->data()),
144 base::Bind(&DriveApiDataRequest::OnDataParsed,
145 weak_ptr_factory_.GetWeakPtr(), error));
146 break;
147 default:
148 RunCallbackOnPrematureFailure(error);
149 OnProcessURLFetchResultsComplete();
150 break;
154 void RunCallbackOnPrematureFailure(DriveApiErrorCode error) override {
155 callback_.Run(error, scoped_ptr<DataType>());
158 private:
159 // Parses the |json| string by using DataType::CreateFrom.
160 static scoped_ptr<DataType> Parse(const std::string& json) {
161 scoped_ptr<base::Value> value = ParseJson(json);
162 return value ? DataType::CreateFrom(*value) : scoped_ptr<DataType>();
165 // Receives the parsed result and invokes the callback.
166 void OnDataParsed(DriveApiErrorCode error, scoped_ptr<DataType> value) {
167 if (!value)
168 error = DRIVE_PARSE_ERROR;
169 callback_.Run(error, value.Pass());
170 OnProcessURLFetchResultsComplete();
173 const Callback callback_;
175 // Note: This should remain the last member so it'll be destroyed and
176 // invalidate its weak pointers before any other members are destroyed.
177 base::WeakPtrFactory<DriveApiDataRequest> weak_ptr_factory_;
179 DISALLOW_COPY_AND_ASSIGN(DriveApiDataRequest);
182 //=============================== FilesGetRequest =============================
184 // This class performs the request for fetching a file.
185 // This request is mapped to
186 // https://developers.google.com/drive/v2/reference/files/get
187 class FilesGetRequest : public DriveApiDataRequest<FileResource> {
188 public:
189 FilesGetRequest(RequestSender* sender,
190 const DriveApiUrlGenerator& url_generator,
191 bool use_internal_endpoint,
192 const FileResourceCallback& callback);
193 ~FilesGetRequest() override;
195 // Required parameter.
196 const std::string& file_id() const { return file_id_; }
197 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
199 // Optional parameter.
200 const GURL& embed_origin() const { return embed_origin_; }
201 void set_embed_origin(const GURL& embed_origin) {
202 embed_origin_ = embed_origin;
205 protected:
206 // Overridden from DriveApiDataRequest.
207 GURL GetURLInternal() const override;
209 private:
210 const DriveApiUrlGenerator url_generator_;
211 const bool use_internal_endpoint_;
212 std::string file_id_;
213 GURL embed_origin_;
215 DISALLOW_COPY_AND_ASSIGN(FilesGetRequest);
218 //============================ FilesAuthorizeRequest ===========================
220 // This class performs request for authorizing an app to access a file.
221 // This request is mapped to /drive/v2internal/file/authorize internal endpoint.
222 class FilesAuthorizeRequest : public DriveApiDataRequest<FileResource> {
223 public:
224 FilesAuthorizeRequest(RequestSender* sender,
225 const DriveApiUrlGenerator& url_generator,
226 const FileResourceCallback& callback);
227 ~FilesAuthorizeRequest() override;
229 // Required parameter.
230 const std::string& file_id() const { return file_id_; }
231 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
232 const std::string& app_id() const { return app_id_; }
233 void set_app_id(const std::string& app_id) { app_id_ = app_id; }
235 protected:
236 // Overridden from GetDataRequest.
237 net::URLFetcher::RequestType GetRequestType() const override;
239 // Overridden from DriveApiDataRequest.
240 GURL GetURLInternal() const override;
242 private:
243 const DriveApiUrlGenerator url_generator_;
244 std::string file_id_;
245 std::string app_id_;
247 DISALLOW_COPY_AND_ASSIGN(FilesAuthorizeRequest);
250 //============================ FilesInsertRequest =============================
252 // Enumeration type for specifying visibility of files.
253 enum FileVisibility {
254 FILE_VISIBILITY_DEFAULT,
255 FILE_VISIBILITY_PRIVATE,
258 // This class performs the request for creating a resource.
259 // This request is mapped to
260 // https://developers.google.com/drive/v2/reference/files/insert
261 // See also https://developers.google.com/drive/manage-uploads and
262 // https://developers.google.com/drive/folder
263 class FilesInsertRequest : public DriveApiDataRequest<FileResource> {
264 public:
265 FilesInsertRequest(RequestSender* sender,
266 const DriveApiUrlGenerator& url_generator,
267 const FileResourceCallback& callback);
268 ~FilesInsertRequest() override;
270 // Optional parameter
271 void set_visibility(FileVisibility visibility) {
272 visibility_ = visibility;
275 // Optional request body.
276 const base::Time& last_viewed_by_me_date() const {
277 return last_viewed_by_me_date_;
279 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
280 last_viewed_by_me_date_ = last_viewed_by_me_date;
283 const std::string& mime_type() const { return mime_type_; }
284 void set_mime_type(const std::string& mime_type) {
285 mime_type_ = mime_type;
288 const base::Time& modified_date() const { return modified_date_; }
289 void set_modified_date(const base::Time& modified_date) {
290 modified_date_ = modified_date;
293 const std::vector<std::string>& parents() const { return parents_; }
294 void add_parent(const std::string& parent) { parents_.push_back(parent); }
296 const std::string& title() const { return title_; }
297 void set_title(const std::string& title) { title_ = title; }
299 const Properties& properties() const { return properties_; }
300 void set_properties(const Properties& properties) {
301 properties_ = properties;
304 protected:
305 // Overridden from GetDataRequest.
306 net::URLFetcher::RequestType GetRequestType() const override;
307 bool GetContentData(std::string* upload_content_type,
308 std::string* upload_content) override;
310 // Overridden from DriveApiDataRequest.
311 GURL GetURLInternal() const override;
313 private:
314 const DriveApiUrlGenerator url_generator_;
316 FileVisibility visibility_;
317 base::Time last_viewed_by_me_date_;
318 std::string mime_type_;
319 base::Time modified_date_;
320 std::vector<std::string> parents_;
321 std::string title_;
322 Properties properties_;
324 DISALLOW_COPY_AND_ASSIGN(FilesInsertRequest);
327 //============================== FilesPatchRequest ============================
329 // This class performs the request for patching file metadata.
330 // This request is mapped to
331 // https://developers.google.com/drive/v2/reference/files/patch
332 class FilesPatchRequest : public DriveApiDataRequest<FileResource> {
333 public:
334 FilesPatchRequest(RequestSender* sender,
335 const DriveApiUrlGenerator& url_generator,
336 const FileResourceCallback& callback);
337 ~FilesPatchRequest() override;
339 // Required parameter.
340 const std::string& file_id() const { return file_id_; }
341 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
343 // Optional parameter.
344 bool set_modified_date() const { return set_modified_date_; }
345 void set_set_modified_date(bool set_modified_date) {
346 set_modified_date_ = set_modified_date;
349 bool update_viewed_date() const { return update_viewed_date_; }
350 void set_update_viewed_date(bool update_viewed_date) {
351 update_viewed_date_ = update_viewed_date;
354 // Optional request body.
355 // Note: "Files: patch" accepts any "Files resource" data, but this class
356 // only supports limited members of it for now. We can extend it upon
357 // requirments.
358 const std::string& title() const { return title_; }
359 void set_title(const std::string& title) { title_ = title; }
361 const base::Time& modified_date() const { return modified_date_; }
362 void set_modified_date(const base::Time& modified_date) {
363 modified_date_ = modified_date;
366 const base::Time& last_viewed_by_me_date() const {
367 return last_viewed_by_me_date_;
369 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
370 last_viewed_by_me_date_ = last_viewed_by_me_date;
373 const std::vector<std::string>& parents() const { return parents_; }
374 void add_parent(const std::string& parent) { parents_.push_back(parent); }
376 const Properties& properties() const { return properties_; }
377 void set_properties(const Properties& properties) {
378 properties_ = properties;
381 protected:
382 // Overridden from URLFetchRequestBase.
383 net::URLFetcher::RequestType GetRequestType() const override;
384 std::vector<std::string> GetExtraRequestHeaders() const override;
385 bool GetContentData(std::string* upload_content_type,
386 std::string* upload_content) override;
388 // Overridden from DriveApiDataRequest.
389 GURL GetURLInternal() const override;
391 private:
392 const DriveApiUrlGenerator url_generator_;
394 std::string file_id_;
395 bool set_modified_date_;
396 bool update_viewed_date_;
398 std::string title_;
399 base::Time modified_date_;
400 base::Time last_viewed_by_me_date_;
401 std::vector<std::string> parents_;
402 Properties properties_;
404 DISALLOW_COPY_AND_ASSIGN(FilesPatchRequest);
407 //============================= FilesCopyRequest ==============================
409 // This class performs the request for copying a resource.
410 // This request is mapped to
411 // https://developers.google.com/drive/v2/reference/files/copy
412 class FilesCopyRequest : public DriveApiDataRequest<FileResource> {
413 public:
414 // Upon completion, |callback| will be called. |callback| must not be null.
415 FilesCopyRequest(RequestSender* sender,
416 const DriveApiUrlGenerator& url_generator,
417 const FileResourceCallback& callback);
418 ~FilesCopyRequest() override;
420 // Required parameter.
421 const std::string& file_id() const { return file_id_; }
422 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
424 // Optional parameter
425 void set_visibility(FileVisibility visibility) {
426 visibility_ = visibility;
429 // Optional request body.
430 const std::vector<std::string>& parents() const { return parents_; }
431 void add_parent(const std::string& parent) { parents_.push_back(parent); }
433 const base::Time& modified_date() const { return modified_date_; }
434 void set_modified_date(const base::Time& modified_date) {
435 modified_date_ = modified_date;
438 const std::string& title() const { return title_; }
439 void set_title(const std::string& title) { title_ = title; }
441 protected:
442 // Overridden from URLFetchRequestBase.
443 net::URLFetcher::RequestType GetRequestType() const override;
444 bool GetContentData(std::string* upload_content_type,
445 std::string* upload_content) override;
447 // Overridden from DriveApiDataRequest.
448 GURL GetURLInternal() const override;
450 private:
451 const DriveApiUrlGenerator url_generator_;
453 std::string file_id_;
454 FileVisibility visibility_;
455 base::Time modified_date_;
456 std::vector<std::string> parents_;
457 std::string title_;
459 DISALLOW_COPY_AND_ASSIGN(FilesCopyRequest);
462 //============================= FilesListRequest =============================
464 // This class performs the request for fetching FileList.
465 // The result may contain only first part of the result. The remaining result
466 // should be able to be fetched by ContinueGetFileListRequest defined below,
467 // or by FilesListRequest with setting page token.
468 // This request is mapped to
469 // https://developers.google.com/drive/v2/reference/files/list
470 class FilesListRequest : public DriveApiDataRequest<FileList> {
471 public:
472 FilesListRequest(RequestSender* sender,
473 const DriveApiUrlGenerator& url_generator,
474 const FileListCallback& callback);
475 ~FilesListRequest() override;
477 // Optional parameter
478 int max_results() const { return max_results_; }
479 void set_max_results(int max_results) { max_results_ = max_results; }
481 const std::string& page_token() const { return page_token_; }
482 void set_page_token(const std::string& page_token) {
483 page_token_ = page_token;
486 const std::string& q() const { return q_; }
487 void set_q(const std::string& q) { q_ = q; }
489 protected:
490 // Overridden from DriveApiDataRequest.
491 GURL GetURLInternal() const override;
493 private:
494 const DriveApiUrlGenerator url_generator_;
495 int max_results_;
496 std::string page_token_;
497 std::string q_;
499 DISALLOW_COPY_AND_ASSIGN(FilesListRequest);
502 //========================= FilesListNextPageRequest ==========================
504 // There are two ways to obtain next pages of "Files: list" result (if paged).
505 // 1) Set pageToken and all params used for the initial request.
506 // 2) Use URL in the nextLink field in the previous response.
507 // This class implements 2)'s request.
508 class FilesListNextPageRequest : public DriveApiDataRequest<FileList> {
509 public:
510 FilesListNextPageRequest(RequestSender* sender,
511 const FileListCallback& callback);
512 ~FilesListNextPageRequest() override;
514 const GURL& next_link() const { return next_link_; }
515 void set_next_link(const GURL& next_link) { next_link_ = next_link; }
517 protected:
518 // Overridden from DriveApiDataRequest.
519 GURL GetURLInternal() const override;
521 private:
522 GURL next_link_;
524 DISALLOW_COPY_AND_ASSIGN(FilesListNextPageRequest);
527 //============================= FilesDeleteRequest =============================
529 // This class performs the request for deleting a resource.
530 // This request is mapped to
531 // https://developers.google.com/drive/v2/reference/files/delete
532 class FilesDeleteRequest : public EntryActionRequest {
533 public:
534 FilesDeleteRequest(RequestSender* sender,
535 const DriveApiUrlGenerator& url_generator,
536 const EntryActionCallback& callback);
537 ~FilesDeleteRequest() override;
539 // Required parameter.
540 const std::string& file_id() const { return file_id_; }
541 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
542 void set_etag(const std::string& etag) { etag_ = etag; }
544 protected:
545 // Overridden from UrlFetchRequestBase.
546 net::URLFetcher::RequestType GetRequestType() const override;
547 GURL GetURL() const override;
548 std::vector<std::string> GetExtraRequestHeaders() const override;
550 private:
551 const DriveApiUrlGenerator url_generator_;
552 std::string file_id_;
553 std::string etag_;
555 DISALLOW_COPY_AND_ASSIGN(FilesDeleteRequest);
558 //============================= FilesTrashRequest ==============================
560 // This class performs the request for trashing a resource.
561 // This request is mapped to
562 // https://developers.google.com/drive/v2/reference/files/trash
563 class FilesTrashRequest : public DriveApiDataRequest<FileResource> {
564 public:
565 FilesTrashRequest(RequestSender* sender,
566 const DriveApiUrlGenerator& url_generator,
567 const FileResourceCallback& callback);
568 ~FilesTrashRequest() override;
570 // Required parameter.
571 const std::string& file_id() const { return file_id_; }
572 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
574 protected:
575 // Overridden from UrlFetchRequestBase.
576 net::URLFetcher::RequestType GetRequestType() const override;
578 // Overridden from DriveApiDataRequest.
579 GURL GetURLInternal() const override;
581 private:
582 const DriveApiUrlGenerator url_generator_;
583 std::string file_id_;
585 DISALLOW_COPY_AND_ASSIGN(FilesTrashRequest);
588 //============================== AboutGetRequest =============================
590 // This class performs the request for fetching About data.
591 // This request is mapped to
592 // https://developers.google.com/drive/v2/reference/about/get
593 class AboutGetRequest : public DriveApiDataRequest<AboutResource> {
594 public:
595 AboutGetRequest(RequestSender* sender,
596 const DriveApiUrlGenerator& url_generator,
597 const AboutResourceCallback& callback);
598 ~AboutGetRequest() override;
600 protected:
601 // Overridden from DriveApiDataRequest.
602 GURL GetURLInternal() const override;
604 private:
605 const DriveApiUrlGenerator url_generator_;
607 DISALLOW_COPY_AND_ASSIGN(AboutGetRequest);
610 //============================ ChangesListRequest ============================
612 // This class performs the request for fetching ChangeList.
613 // The result may contain only first part of the result. The remaining result
614 // should be able to be fetched by ContinueGetFileListRequest defined below.
615 // or by ChangesListRequest with setting page token.
616 // This request is mapped to
617 // https://developers.google.com/drive/v2/reference/changes/list
618 class ChangesListRequest : public DriveApiDataRequest<ChangeList> {
619 public:
620 ChangesListRequest(RequestSender* sender,
621 const DriveApiUrlGenerator& url_generator,
622 const ChangeListCallback& callback);
623 ~ChangesListRequest() override;
625 // Optional parameter
626 bool include_deleted() const { return include_deleted_; }
627 void set_include_deleted(bool include_deleted) {
628 include_deleted_ = include_deleted;
631 int max_results() const { return max_results_; }
632 void set_max_results(int max_results) { max_results_ = max_results; }
634 const std::string& page_token() const { return page_token_; }
635 void set_page_token(const std::string& page_token) {
636 page_token_ = page_token;
639 int64 start_change_id() const { return start_change_id_; }
640 void set_start_change_id(int64 start_change_id) {
641 start_change_id_ = start_change_id;
644 protected:
645 // Overridden from DriveApiDataRequest.
646 GURL GetURLInternal() const override;
648 private:
649 const DriveApiUrlGenerator url_generator_;
650 bool include_deleted_;
651 int max_results_;
652 std::string page_token_;
653 int64 start_change_id_;
655 DISALLOW_COPY_AND_ASSIGN(ChangesListRequest);
658 //======================== ChangesListNextPageRequest =========================
660 // There are two ways to obtain next pages of "Changes: list" result (if paged).
661 // 1) Set pageToken and all params used for the initial request.
662 // 2) Use URL in the nextLink field in the previous response.
663 // This class implements 2)'s request.
664 class ChangesListNextPageRequest : public DriveApiDataRequest<ChangeList> {
665 public:
666 ChangesListNextPageRequest(RequestSender* sender,
667 const ChangeListCallback& callback);
668 ~ChangesListNextPageRequest() override;
670 const GURL& next_link() const { return next_link_; }
671 void set_next_link(const GURL& next_link) { next_link_ = next_link; }
673 protected:
674 // Overridden from DriveApiDataRequest.
675 GURL GetURLInternal() const override;
677 private:
678 GURL next_link_;
680 DISALLOW_COPY_AND_ASSIGN(ChangesListNextPageRequest);
683 //============================= AppsListRequest ============================
685 // This class performs the request for fetching AppList.
686 // This request is mapped to
687 // https://developers.google.com/drive/v2/reference/apps/list
688 class AppsListRequest : public DriveApiDataRequest<AppList> {
689 public:
690 AppsListRequest(RequestSender* sender,
691 const DriveApiUrlGenerator& url_generator,
692 bool use_internal_endpoint,
693 const AppListCallback& callback);
694 ~AppsListRequest() override;
696 protected:
697 // Overridden from DriveApiDataRequest.
698 GURL GetURLInternal() const override;
700 private:
701 const DriveApiUrlGenerator url_generator_;
702 const bool use_internal_endpoint_;
704 DISALLOW_COPY_AND_ASSIGN(AppsListRequest);
707 //============================= AppsDeleteRequest ==============================
709 // This class performs the request for deleting a Drive app.
710 // This request is mapped to
711 // https://developers.google.com/drive/v2/reference/files/trash
712 class AppsDeleteRequest : public EntryActionRequest {
713 public:
714 AppsDeleteRequest(RequestSender* sender,
715 const DriveApiUrlGenerator& url_generator,
716 const EntryActionCallback& callback);
717 ~AppsDeleteRequest() override;
719 // Required parameter.
720 const std::string& app_id() const { return app_id_; }
721 void set_app_id(const std::string& app_id) { app_id_ = app_id; }
723 protected:
724 // Overridden from UrlFetchRequestBase.
725 net::URLFetcher::RequestType GetRequestType() const override;
726 GURL GetURL() const override;
728 private:
729 const DriveApiUrlGenerator url_generator_;
730 std::string app_id_;
732 DISALLOW_COPY_AND_ASSIGN(AppsDeleteRequest);
735 //========================== ChildrenInsertRequest ============================
737 // This class performs the request for inserting a resource to a directory.
738 // This request is mapped to
739 // https://developers.google.com/drive/v2/reference/children/insert
740 class ChildrenInsertRequest : public EntryActionRequest {
741 public:
742 ChildrenInsertRequest(RequestSender* sender,
743 const DriveApiUrlGenerator& url_generator,
744 const EntryActionCallback& callback);
745 ~ChildrenInsertRequest() override;
747 // Required parameter.
748 const std::string& folder_id() const { return folder_id_; }
749 void set_folder_id(const std::string& folder_id) {
750 folder_id_ = folder_id;
753 // Required body.
754 const std::string& id() const { return id_; }
755 void set_id(const std::string& id) { id_ = id; }
757 protected:
758 // UrlFetchRequestBase overrides.
759 net::URLFetcher::RequestType GetRequestType() const override;
760 GURL GetURL() const override;
761 bool GetContentData(std::string* upload_content_type,
762 std::string* upload_content) override;
764 private:
765 const DriveApiUrlGenerator url_generator_;
766 std::string folder_id_;
767 std::string id_;
769 DISALLOW_COPY_AND_ASSIGN(ChildrenInsertRequest);
772 //========================== ChildrenDeleteRequest ============================
774 // This class performs the request for removing a resource from a directory.
775 // This request is mapped to
776 // https://developers.google.com/drive/v2/reference/children/delete
777 class ChildrenDeleteRequest : public EntryActionRequest {
778 public:
779 // |callback| must not be null.
780 ChildrenDeleteRequest(RequestSender* sender,
781 const DriveApiUrlGenerator& url_generator,
782 const EntryActionCallback& callback);
783 ~ChildrenDeleteRequest() override;
785 // Required parameter.
786 const std::string& child_id() const { return child_id_; }
787 void set_child_id(const std::string& child_id) {
788 child_id_ = child_id;
791 const std::string& folder_id() const { return folder_id_; }
792 void set_folder_id(const std::string& folder_id) {
793 folder_id_ = folder_id;
796 protected:
797 // UrlFetchRequestBase overrides.
798 net::URLFetcher::RequestType GetRequestType() const override;
799 GURL GetURL() const override;
801 private:
802 const DriveApiUrlGenerator url_generator_;
803 std::string child_id_;
804 std::string folder_id_;
806 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleteRequest);
809 //======================= InitiateUploadNewFileRequest =======================
811 // This class performs the request for initiating the upload of a new file.
812 class InitiateUploadNewFileRequest : public InitiateUploadRequestBase {
813 public:
814 // |parent_resource_id| should be the resource id of the parent directory.
815 // |title| should be set.
816 // See also the comments of InitiateUploadRequestBase for more details
817 // about the other parameters.
818 InitiateUploadNewFileRequest(RequestSender* sender,
819 const DriveApiUrlGenerator& url_generator,
820 const std::string& content_type,
821 int64 content_length,
822 const std::string& parent_resource_id,
823 const std::string& title,
824 const InitiateUploadCallback& callback);
825 ~InitiateUploadNewFileRequest() override;
827 // Optional parameters.
828 const base::Time& modified_date() const { return modified_date_; }
829 void set_modified_date(const base::Time& modified_date) {
830 modified_date_ = modified_date;
832 const base::Time& last_viewed_by_me_date() const {
833 return last_viewed_by_me_date_;
835 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
836 last_viewed_by_me_date_ = last_viewed_by_me_date;
838 const Properties& properties() const { return properties_; }
839 void set_properties(const Properties& properties) {
840 properties_ = properties;
843 protected:
844 // UrlFetchRequestBase overrides.
845 GURL GetURL() const override;
846 net::URLFetcher::RequestType GetRequestType() const override;
847 bool GetContentData(std::string* upload_content_type,
848 std::string* upload_content) override;
850 private:
851 const DriveApiUrlGenerator url_generator_;
852 const std::string parent_resource_id_;
853 const std::string title_;
855 base::Time modified_date_;
856 base::Time last_viewed_by_me_date_;
857 Properties properties_;
859 DISALLOW_COPY_AND_ASSIGN(InitiateUploadNewFileRequest);
862 //==================== InitiateUploadExistingFileRequest =====================
864 // This class performs the request for initiating the upload of an existing
865 // file.
866 class InitiateUploadExistingFileRequest : public InitiateUploadRequestBase {
867 public:
868 // |upload_url| should be the upload_url() of the file
869 // (resumable-create-media URL)
870 // |etag| should be set if it is available to detect the upload confliction.
871 // See also the comments of InitiateUploadRequestBase for more details
872 // about the other parameters.
873 InitiateUploadExistingFileRequest(RequestSender* sender,
874 const DriveApiUrlGenerator& url_generator,
875 const std::string& content_type,
876 int64 content_length,
877 const std::string& resource_id,
878 const std::string& etag,
879 const InitiateUploadCallback& callback);
880 ~InitiateUploadExistingFileRequest() override;
882 // Optional parameters.
883 const std::string& parent_resource_id() const { return parent_resource_id_; }
884 void set_parent_resource_id(const std::string& parent_resource_id) {
885 parent_resource_id_ = parent_resource_id;
887 const std::string& title() const { return title_; }
888 void set_title(const std::string& title) { title_ = title; }
889 const base::Time& modified_date() const { return modified_date_; }
890 void set_modified_date(const base::Time& modified_date) {
891 modified_date_ = modified_date;
893 const base::Time& last_viewed_by_me_date() const {
894 return last_viewed_by_me_date_;
896 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
897 last_viewed_by_me_date_ = last_viewed_by_me_date;
899 const Properties& properties() const { return properties_; }
900 void set_properties(const Properties& properties) {
901 properties_ = properties;
904 protected:
905 // UrlFetchRequestBase overrides.
906 GURL GetURL() const override;
907 net::URLFetcher::RequestType GetRequestType() const override;
908 std::vector<std::string> GetExtraRequestHeaders() const override;
909 bool GetContentData(std::string* upload_content_type,
910 std::string* upload_content) override;
912 private:
913 const DriveApiUrlGenerator url_generator_;
914 const std::string resource_id_;
915 const std::string etag_;
917 std::string parent_resource_id_;
918 std::string title_;
919 base::Time modified_date_;
920 base::Time last_viewed_by_me_date_;
921 Properties properties_;
923 DISALLOW_COPY_AND_ASSIGN(InitiateUploadExistingFileRequest);
926 // Callback used for ResumeUpload() and GetUploadStatus().
927 typedef base::Callback<void(
928 const UploadRangeResponse& response,
929 scoped_ptr<FileResource> new_resource)> UploadRangeCallback;
931 //============================ ResumeUploadRequest ===========================
933 // Performs the request for resuming the upload of a file.
934 class ResumeUploadRequest : public ResumeUploadRequestBase {
935 public:
936 // See also ResumeUploadRequestBase's comment for parameters meaning.
937 // |callback| must not be null. |progress_callback| may be null.
938 ResumeUploadRequest(RequestSender* sender,
939 const GURL& upload_location,
940 int64 start_position,
941 int64 end_position,
942 int64 content_length,
943 const std::string& content_type,
944 const base::FilePath& local_file_path,
945 const UploadRangeCallback& callback,
946 const ProgressCallback& progress_callback);
947 ~ResumeUploadRequest() override;
949 protected:
950 // UploadRangeRequestBase overrides.
951 void OnRangeRequestComplete(const UploadRangeResponse& response,
952 scoped_ptr<base::Value> value) override;
953 // content::UrlFetcherDelegate overrides.
954 void OnURLFetchUploadProgress(const net::URLFetcher* source,
955 int64 current,
956 int64 total) override;
958 private:
959 const UploadRangeCallback callback_;
960 const ProgressCallback progress_callback_;
962 DISALLOW_COPY_AND_ASSIGN(ResumeUploadRequest);
965 //========================== GetUploadStatusRequest ==========================
967 // Performs the request to fetch the current upload status of a file.
968 class GetUploadStatusRequest : public GetUploadStatusRequestBase {
969 public:
970 // See also GetUploadStatusRequestBase's comment for parameters meaning.
971 // |callback| must not be null.
972 GetUploadStatusRequest(RequestSender* sender,
973 const GURL& upload_url,
974 int64 content_length,
975 const UploadRangeCallback& callback);
976 ~GetUploadStatusRequest() override;
978 protected:
979 // UploadRangeRequestBase overrides.
980 void OnRangeRequestComplete(const UploadRangeResponse& response,
981 scoped_ptr<base::Value> value) override;
983 private:
984 const UploadRangeCallback callback_;
986 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequest);
989 //======================= MultipartUploadNewFileDelegate =======================
991 // This class performs the request for initiating the upload of a new file.
992 class MultipartUploadNewFileDelegate : public MultipartUploadRequestBase {
993 public:
994 // |parent_resource_id| should be the resource id of the parent directory.
995 // |title| should be set.
996 // See also the comments of MultipartUploadRequestBase for more details
997 // about the other parameters.
998 MultipartUploadNewFileDelegate(base::SequencedTaskRunner* task_runner,
999 const std::string& title,
1000 const std::string& parent_resource_id,
1001 const std::string& content_type,
1002 int64 content_length,
1003 const base::Time& modified_date,
1004 const base::Time& last_viewed_by_me_date,
1005 const base::FilePath& local_file_path,
1006 const Properties& properties,
1007 const DriveApiUrlGenerator& url_generator,
1008 const FileResourceCallback& callback,
1009 const ProgressCallback& progress_callback);
1010 ~MultipartUploadNewFileDelegate() override;
1012 protected:
1013 // UrlFetchRequestBase overrides.
1014 GURL GetURL() const override;
1015 net::URLFetcher::RequestType GetRequestType() const override;
1017 private:
1018 const bool has_modified_date_;
1019 const DriveApiUrlGenerator url_generator_;
1021 DISALLOW_COPY_AND_ASSIGN(MultipartUploadNewFileDelegate);
1024 //====================== MultipartUploadExistingFileDelegate ===================
1026 // This class performs the request for initiating the upload of a new file.
1027 class MultipartUploadExistingFileDelegate : public MultipartUploadRequestBase {
1028 public:
1029 // |parent_resource_id| should be the resource id of the parent directory.
1030 // |title| should be set.
1031 // See also the comments of MultipartUploadRequestBase for more details
1032 // about the other parameters.
1033 MultipartUploadExistingFileDelegate(
1034 base::SequencedTaskRunner* task_runner,
1035 const std::string& title,
1036 const std::string& resource_id,
1037 const std::string& parent_resource_id,
1038 const std::string& content_type,
1039 int64 content_length,
1040 const base::Time& modified_date,
1041 const base::Time& last_viewed_by_me_date,
1042 const base::FilePath& local_file_path,
1043 const std::string& etag,
1044 const Properties& properties,
1045 const DriveApiUrlGenerator& url_generator,
1046 const FileResourceCallback& callback,
1047 const ProgressCallback& progress_callback);
1048 ~MultipartUploadExistingFileDelegate() override;
1050 protected:
1051 // UrlFetchRequestBase overrides.
1052 std::vector<std::string> GetExtraRequestHeaders() const override;
1053 GURL GetURL() const override;
1054 net::URLFetcher::RequestType GetRequestType() const override;
1056 private:
1057 const std::string resource_id_;
1058 const std::string etag_;
1059 const bool has_modified_date_;
1060 const DriveApiUrlGenerator url_generator_;
1062 DISALLOW_COPY_AND_ASSIGN(MultipartUploadExistingFileDelegate);
1065 //========================== DownloadFileRequest ==========================
1067 // This class performs the request for downloading of a specified file.
1068 class DownloadFileRequest : public DownloadFileRequestBase {
1069 public:
1070 // See also DownloadFileRequestBase's comment for parameters meaning.
1071 DownloadFileRequest(RequestSender* sender,
1072 const DriveApiUrlGenerator& url_generator,
1073 const std::string& resource_id,
1074 const base::FilePath& output_file_path,
1075 const DownloadActionCallback& download_action_callback,
1076 const GetContentCallback& get_content_callback,
1077 const ProgressCallback& progress_callback);
1078 ~DownloadFileRequest() override;
1080 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest);
1083 //========================== PermissionsInsertRequest ==========================
1085 // Enumeration type for specifying type of permissions.
1086 enum PermissionType {
1087 PERMISSION_TYPE_ANYONE,
1088 PERMISSION_TYPE_DOMAIN,
1089 PERMISSION_TYPE_GROUP,
1090 PERMISSION_TYPE_USER,
1093 // Enumeration type for specifying the role of permissions.
1094 enum PermissionRole {
1095 PERMISSION_ROLE_OWNER,
1096 PERMISSION_ROLE_READER,
1097 PERMISSION_ROLE_WRITER,
1098 PERMISSION_ROLE_COMMENTER,
1101 // This class performs the request for adding permission on a specified file.
1102 class PermissionsInsertRequest : public EntryActionRequest {
1103 public:
1104 // See https://developers.google.com/drive/v2/reference/permissions/insert.
1105 PermissionsInsertRequest(RequestSender* sender,
1106 const DriveApiUrlGenerator& url_generator,
1107 const EntryActionCallback& callback);
1108 ~PermissionsInsertRequest() override;
1110 void set_id(const std::string& id) { id_ = id; }
1111 void set_type(PermissionType type) { type_ = type; }
1112 void set_role(PermissionRole role) { role_ = role; }
1113 void set_value(const std::string& value) { value_ = value; }
1115 // UrlFetchRequestBase overrides.
1116 GURL GetURL() const override;
1117 net::URLFetcher::RequestType GetRequestType() const override;
1118 bool GetContentData(std::string* upload_content_type,
1119 std::string* upload_content) override;
1121 private:
1122 const DriveApiUrlGenerator url_generator_;
1123 std::string id_;
1124 PermissionType type_;
1125 PermissionRole role_;
1126 std::string value_;
1128 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest);
1131 //======================= SingleBatchableDelegateRequest =======================
1133 // Request that is operated by single BatchableDelegate.
1134 class SingleBatchableDelegateRequest : public UrlFetchRequestBase {
1135 public:
1136 // The instance takes ownership of |delegate|.
1137 SingleBatchableDelegateRequest(RequestSender* sender,
1138 BatchableDelegate* delegate);
1139 ~SingleBatchableDelegateRequest() override;
1141 private:
1142 GURL GetURL() const override;
1143 net::URLFetcher::RequestType GetRequestType() const override;
1144 std::vector<std::string> GetExtraRequestHeaders() const override;
1145 void Prepare(const PrepareCallback& callback) override;
1146 bool GetContentData(std::string* upload_content_type,
1147 std::string* upload_content) override;
1148 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override;
1149 void ProcessURLFetchResults(const net::URLFetcher* source) override;
1150 void OnURLFetchUploadProgress(const net::URLFetcher* source,
1151 int64 current,
1152 int64 total) override;
1153 scoped_ptr<BatchableDelegate> delegate_;
1155 // Note: This should remain the last member so it'll be destroyed and
1156 // invalidate its weak pointers before any other members are destroyed.
1157 base::WeakPtrFactory<SingleBatchableDelegateRequest> weak_ptr_factory_;
1159 DISALLOW_COPY_AND_ASSIGN(SingleBatchableDelegateRequest);
1162 //========================== BatchUploadRequest ==========================
1164 class BatchUploadChildEntry {
1165 public:
1166 explicit BatchUploadChildEntry(BatchableDelegate* request);
1167 ~BatchUploadChildEntry();
1168 scoped_ptr<BatchableDelegate> request;
1169 bool prepared;
1170 int64 data_offset;
1171 int64 data_size;
1173 private:
1174 DISALLOW_COPY_AND_ASSIGN(BatchUploadChildEntry);
1177 class BatchUploadRequest : public UrlFetchRequestBase {
1178 public:
1179 BatchUploadRequest(RequestSender* sender,
1180 const DriveApiUrlGenerator& url_generator);
1181 ~BatchUploadRequest() override;
1183 // Adds request to the batch request. The instance takes ownership of
1184 // |request|.
1185 void AddRequest(BatchableDelegate* request);
1187 // Completes building batch upload request, and starts to send the request to
1188 // server. Must add at least one request before calling |Commit|.
1189 void Commit();
1191 // Obtains weak pointer of this.
1192 base::WeakPtr<BatchUploadRequest> GetWeakPtrAsBatchUploadRequest();
1194 // Set boundary. Only tests can use this method.
1195 void SetBoundaryForTesting(const std::string& boundary);
1197 // Obtains reference to RequestSender that owns the request.
1198 RequestSender* sender() const { return sender_; }
1200 // Obtains URLGenerator.
1201 const DriveApiUrlGenerator& url_generator() const { return url_generator_; }
1203 // UrlFetchRequestBase overrides.
1204 void Prepare(const PrepareCallback& callback) override;
1205 void Cancel() override;
1206 GURL GetURL() const override;
1207 net::URLFetcher::RequestType GetRequestType() const override;
1208 std::vector<std::string> GetExtraRequestHeaders() const override;
1209 bool GetContentData(std::string* upload_content_type,
1210 std::string* upload_content) override;
1211 void ProcessURLFetchResults(const net::URLFetcher* source) override;
1212 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override;
1214 // content::UrlFetcherDelegate overrides.
1215 void OnURLFetchUploadProgress(const net::URLFetcher* source,
1216 int64 current,
1217 int64 total) override;
1219 private:
1220 typedef void* RequestID;
1221 // Obtains corresponding child entry of |request_id|. Returns NULL if the
1222 // entry is not found.
1223 ScopedVector<BatchUploadChildEntry>::iterator GetChildEntry(
1224 RequestID request_id);
1226 // Called after child requests' |Prepare| method.
1227 void OnChildRequestPrepared(RequestID request_id, DriveApiErrorCode result);
1229 // Complete |Prepare| if possible.
1230 void MayCompletePrepare();
1232 // Process result for each child.
1233 void ProcessURLFetchResultsForChild(RequestID id, const std::string& body);
1235 RequestSender* const sender_;
1236 const DriveApiUrlGenerator url_generator_;
1237 ScopedVector<BatchUploadChildEntry> child_requests_;
1239 PrepareCallback prepare_callback_;
1240 bool committed_;
1242 // Boundary of multipart body.
1243 std::string boundary_;
1245 // Multipart of child requests.
1246 ContentTypeAndData upload_content_;
1248 // Last reported progress value.
1249 int64 last_progress_value_;
1251 // Note: This should remain the last member so it'll be destroyed and
1252 // invalidate its weak pointers before any other members are destroyed.
1253 base::WeakPtrFactory<BatchUploadRequest> weak_ptr_factory_;
1255 DISALLOW_COPY_AND_ASSIGN(BatchUploadRequest);
1258 } // namespace drive
1259 } // namespace google_apis
1261 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_