Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / google_apis / drive / drive_api_requests.h
blob8ce6ad19b8ecb86ac686a3efc7d86dfcdac0cba5
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 // This class performs the request for creating a resource.
253 // This request is mapped to
254 // https://developers.google.com/drive/v2/reference/files/insert
255 // See also https://developers.google.com/drive/manage-uploads and
256 // https://developers.google.com/drive/folder
257 class FilesInsertRequest : public DriveApiDataRequest<FileResource> {
258 public:
259 FilesInsertRequest(RequestSender* sender,
260 const DriveApiUrlGenerator& url_generator,
261 const FileResourceCallback& callback);
262 ~FilesInsertRequest() override;
264 // Optional parameter
265 const std::string& visibility() const { return visibility_; }
266 void set_visibility(const std::string& visibility) {
267 visibility_ = visibility;
270 // Optional request body.
271 const base::Time& last_viewed_by_me_date() const {
272 return last_viewed_by_me_date_;
274 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
275 last_viewed_by_me_date_ = last_viewed_by_me_date;
278 const std::string& mime_type() const { return mime_type_; }
279 void set_mime_type(const std::string& mime_type) {
280 mime_type_ = mime_type;
283 const base::Time& modified_date() const { return modified_date_; }
284 void set_modified_date(const base::Time& modified_date) {
285 modified_date_ = modified_date;
288 const std::vector<std::string>& parents() const { return parents_; }
289 void add_parent(const std::string& parent) { parents_.push_back(parent); }
291 const std::string& title() const { return title_; }
292 void set_title(const std::string& title) { title_ = title; }
294 const Properties& properties() const { return properties_; }
295 void set_properties(const Properties& properties) {
296 properties_ = properties;
299 protected:
300 // Overridden from GetDataRequest.
301 net::URLFetcher::RequestType GetRequestType() const override;
302 bool GetContentData(std::string* upload_content_type,
303 std::string* upload_content) override;
305 // Overridden from DriveApiDataRequest.
306 GURL GetURLInternal() const override;
308 private:
309 const DriveApiUrlGenerator url_generator_;
311 std::string visibility_;
312 base::Time last_viewed_by_me_date_;
313 std::string mime_type_;
314 base::Time modified_date_;
315 std::vector<std::string> parents_;
316 std::string title_;
317 Properties properties_;
319 DISALLOW_COPY_AND_ASSIGN(FilesInsertRequest);
322 //============================== FilesPatchRequest ============================
324 // This class performs the request for patching file metadata.
325 // This request is mapped to
326 // https://developers.google.com/drive/v2/reference/files/patch
327 class FilesPatchRequest : public DriveApiDataRequest<FileResource> {
328 public:
329 FilesPatchRequest(RequestSender* sender,
330 const DriveApiUrlGenerator& url_generator,
331 const FileResourceCallback& callback);
332 ~FilesPatchRequest() override;
334 // Required parameter.
335 const std::string& file_id() const { return file_id_; }
336 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
338 // Optional parameter.
339 bool set_modified_date() const { return set_modified_date_; }
340 void set_set_modified_date(bool set_modified_date) {
341 set_modified_date_ = set_modified_date;
344 bool update_viewed_date() const { return update_viewed_date_; }
345 void set_update_viewed_date(bool update_viewed_date) {
346 update_viewed_date_ = update_viewed_date;
349 // Optional request body.
350 // Note: "Files: patch" accepts any "Files resource" data, but this class
351 // only supports limited members of it for now. We can extend it upon
352 // requirments.
353 const std::string& title() const { return title_; }
354 void set_title(const std::string& title) { title_ = title; }
356 const base::Time& modified_date() const { return modified_date_; }
357 void set_modified_date(const base::Time& modified_date) {
358 modified_date_ = modified_date;
361 const base::Time& last_viewed_by_me_date() const {
362 return last_viewed_by_me_date_;
364 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
365 last_viewed_by_me_date_ = last_viewed_by_me_date;
368 const std::vector<std::string>& parents() const { return parents_; }
369 void add_parent(const std::string& parent) { parents_.push_back(parent); }
371 const Properties& properties() const { return properties_; }
372 void set_properties(const Properties& properties) {
373 properties_ = properties;
376 protected:
377 // Overridden from URLFetchRequestBase.
378 net::URLFetcher::RequestType GetRequestType() const override;
379 std::vector<std::string> GetExtraRequestHeaders() const override;
380 bool GetContentData(std::string* upload_content_type,
381 std::string* upload_content) override;
383 // Overridden from DriveApiDataRequest.
384 GURL GetURLInternal() const override;
386 private:
387 const DriveApiUrlGenerator url_generator_;
389 std::string file_id_;
390 bool set_modified_date_;
391 bool update_viewed_date_;
393 std::string title_;
394 base::Time modified_date_;
395 base::Time last_viewed_by_me_date_;
396 std::vector<std::string> parents_;
397 Properties properties_;
399 DISALLOW_COPY_AND_ASSIGN(FilesPatchRequest);
402 //============================= FilesCopyRequest ==============================
404 // This class performs the request for copying a resource.
405 // This request is mapped to
406 // https://developers.google.com/drive/v2/reference/files/copy
407 class FilesCopyRequest : public DriveApiDataRequest<FileResource> {
408 public:
409 // Upon completion, |callback| will be called. |callback| must not be null.
410 FilesCopyRequest(RequestSender* sender,
411 const DriveApiUrlGenerator& url_generator,
412 const FileResourceCallback& callback);
413 ~FilesCopyRequest() override;
415 // Required parameter.
416 const std::string& file_id() const { return file_id_; }
417 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
419 // Optional parameter
420 const std::string& visibility() const { return visibility_; }
421 void set_visibility(const std::string& visibility) {
422 visibility_ = visibility;
425 // Optional request body.
426 const std::vector<std::string>& parents() const { return parents_; }
427 void add_parent(const std::string& parent) { parents_.push_back(parent); }
429 const base::Time& modified_date() const { return modified_date_; }
430 void set_modified_date(const base::Time& modified_date) {
431 modified_date_ = modified_date;
434 const std::string& title() const { return title_; }
435 void set_title(const std::string& title) { title_ = title; }
437 protected:
438 // Overridden from URLFetchRequestBase.
439 net::URLFetcher::RequestType GetRequestType() const override;
440 bool GetContentData(std::string* upload_content_type,
441 std::string* upload_content) override;
443 // Overridden from DriveApiDataRequest.
444 GURL GetURLInternal() const override;
446 private:
447 const DriveApiUrlGenerator url_generator_;
449 std::string file_id_;
450 std::string visibility_;
451 base::Time modified_date_;
452 std::vector<std::string> parents_;
453 std::string title_;
455 DISALLOW_COPY_AND_ASSIGN(FilesCopyRequest);
458 //============================= FilesListRequest =============================
460 // This class performs the request for fetching FileList.
461 // The result may contain only first part of the result. The remaining result
462 // should be able to be fetched by ContinueGetFileListRequest defined below,
463 // or by FilesListRequest with setting page token.
464 // This request is mapped to
465 // https://developers.google.com/drive/v2/reference/files/list
466 class FilesListRequest : public DriveApiDataRequest<FileList> {
467 public:
468 FilesListRequest(RequestSender* sender,
469 const DriveApiUrlGenerator& url_generator,
470 const FileListCallback& callback);
471 ~FilesListRequest() override;
473 // Optional parameter
474 int max_results() const { return max_results_; }
475 void set_max_results(int max_results) { max_results_ = max_results; }
477 const std::string& page_token() const { return page_token_; }
478 void set_page_token(const std::string& page_token) {
479 page_token_ = page_token;
482 const std::string& q() const { return q_; }
483 void set_q(const std::string& q) { q_ = q; }
485 protected:
486 // Overridden from DriveApiDataRequest.
487 GURL GetURLInternal() const override;
489 private:
490 const DriveApiUrlGenerator url_generator_;
491 int max_results_;
492 std::string page_token_;
493 std::string q_;
495 DISALLOW_COPY_AND_ASSIGN(FilesListRequest);
498 //========================= FilesListNextPageRequest ==========================
500 // There are two ways to obtain next pages of "Files: list" result (if paged).
501 // 1) Set pageToken and all params used for the initial request.
502 // 2) Use URL in the nextLink field in the previous response.
503 // This class implements 2)'s request.
504 class FilesListNextPageRequest : public DriveApiDataRequest<FileList> {
505 public:
506 FilesListNextPageRequest(RequestSender* sender,
507 const FileListCallback& callback);
508 ~FilesListNextPageRequest() override;
510 const GURL& next_link() const { return next_link_; }
511 void set_next_link(const GURL& next_link) { next_link_ = next_link; }
513 protected:
514 // Overridden from DriveApiDataRequest.
515 GURL GetURLInternal() const override;
517 private:
518 GURL next_link_;
520 DISALLOW_COPY_AND_ASSIGN(FilesListNextPageRequest);
523 //============================= FilesDeleteRequest =============================
525 // This class performs the request for deleting a resource.
526 // This request is mapped to
527 // https://developers.google.com/drive/v2/reference/files/delete
528 class FilesDeleteRequest : public EntryActionRequest {
529 public:
530 FilesDeleteRequest(RequestSender* sender,
531 const DriveApiUrlGenerator& url_generator,
532 const EntryActionCallback& callback);
533 ~FilesDeleteRequest() override;
535 // Required parameter.
536 const std::string& file_id() const { return file_id_; }
537 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
538 void set_etag(const std::string& etag) { etag_ = etag; }
540 protected:
541 // Overridden from UrlFetchRequestBase.
542 net::URLFetcher::RequestType GetRequestType() const override;
543 GURL GetURL() const override;
544 std::vector<std::string> GetExtraRequestHeaders() const override;
546 private:
547 const DriveApiUrlGenerator url_generator_;
548 std::string file_id_;
549 std::string etag_;
551 DISALLOW_COPY_AND_ASSIGN(FilesDeleteRequest);
554 //============================= FilesTrashRequest ==============================
556 // This class performs the request for trashing a resource.
557 // This request is mapped to
558 // https://developers.google.com/drive/v2/reference/files/trash
559 class FilesTrashRequest : public DriveApiDataRequest<FileResource> {
560 public:
561 FilesTrashRequest(RequestSender* sender,
562 const DriveApiUrlGenerator& url_generator,
563 const FileResourceCallback& callback);
564 ~FilesTrashRequest() override;
566 // Required parameter.
567 const std::string& file_id() const { return file_id_; }
568 void set_file_id(const std::string& file_id) { file_id_ = file_id; }
570 protected:
571 // Overridden from UrlFetchRequestBase.
572 net::URLFetcher::RequestType GetRequestType() const override;
574 // Overridden from DriveApiDataRequest.
575 GURL GetURLInternal() const override;
577 private:
578 const DriveApiUrlGenerator url_generator_;
579 std::string file_id_;
581 DISALLOW_COPY_AND_ASSIGN(FilesTrashRequest);
584 //============================== AboutGetRequest =============================
586 // This class performs the request for fetching About data.
587 // This request is mapped to
588 // https://developers.google.com/drive/v2/reference/about/get
589 class AboutGetRequest : public DriveApiDataRequest<AboutResource> {
590 public:
591 AboutGetRequest(RequestSender* sender,
592 const DriveApiUrlGenerator& url_generator,
593 const AboutResourceCallback& callback);
594 ~AboutGetRequest() override;
596 protected:
597 // Overridden from DriveApiDataRequest.
598 GURL GetURLInternal() const override;
600 private:
601 const DriveApiUrlGenerator url_generator_;
603 DISALLOW_COPY_AND_ASSIGN(AboutGetRequest);
606 //============================ ChangesListRequest ============================
608 // This class performs the request for fetching ChangeList.
609 // The result may contain only first part of the result. The remaining result
610 // should be able to be fetched by ContinueGetFileListRequest defined below.
611 // or by ChangesListRequest with setting page token.
612 // This request is mapped to
613 // https://developers.google.com/drive/v2/reference/changes/list
614 class ChangesListRequest : public DriveApiDataRequest<ChangeList> {
615 public:
616 ChangesListRequest(RequestSender* sender,
617 const DriveApiUrlGenerator& url_generator,
618 const ChangeListCallback& callback);
619 ~ChangesListRequest() override;
621 // Optional parameter
622 bool include_deleted() const { return include_deleted_; }
623 void set_include_deleted(bool include_deleted) {
624 include_deleted_ = include_deleted;
627 int max_results() const { return max_results_; }
628 void set_max_results(int max_results) { max_results_ = max_results; }
630 const std::string& page_token() const { return page_token_; }
631 void set_page_token(const std::string& page_token) {
632 page_token_ = page_token;
635 int64 start_change_id() const { return start_change_id_; }
636 void set_start_change_id(int64 start_change_id) {
637 start_change_id_ = start_change_id;
640 protected:
641 // Overridden from DriveApiDataRequest.
642 GURL GetURLInternal() const override;
644 private:
645 const DriveApiUrlGenerator url_generator_;
646 bool include_deleted_;
647 int max_results_;
648 std::string page_token_;
649 int64 start_change_id_;
651 DISALLOW_COPY_AND_ASSIGN(ChangesListRequest);
654 //======================== ChangesListNextPageRequest =========================
656 // There are two ways to obtain next pages of "Changes: list" result (if paged).
657 // 1) Set pageToken and all params used for the initial request.
658 // 2) Use URL in the nextLink field in the previous response.
659 // This class implements 2)'s request.
660 class ChangesListNextPageRequest : public DriveApiDataRequest<ChangeList> {
661 public:
662 ChangesListNextPageRequest(RequestSender* sender,
663 const ChangeListCallback& callback);
664 ~ChangesListNextPageRequest() override;
666 const GURL& next_link() const { return next_link_; }
667 void set_next_link(const GURL& next_link) { next_link_ = next_link; }
669 protected:
670 // Overridden from DriveApiDataRequest.
671 GURL GetURLInternal() const override;
673 private:
674 GURL next_link_;
676 DISALLOW_COPY_AND_ASSIGN(ChangesListNextPageRequest);
679 //============================= AppsListRequest ============================
681 // This class performs the request for fetching AppList.
682 // This request is mapped to
683 // https://developers.google.com/drive/v2/reference/apps/list
684 class AppsListRequest : public DriveApiDataRequest<AppList> {
685 public:
686 AppsListRequest(RequestSender* sender,
687 const DriveApiUrlGenerator& url_generator,
688 bool use_internal_endpoint,
689 const AppListCallback& callback);
690 ~AppsListRequest() override;
692 protected:
693 // Overridden from DriveApiDataRequest.
694 GURL GetURLInternal() const override;
696 private:
697 const DriveApiUrlGenerator url_generator_;
698 const bool use_internal_endpoint_;
700 DISALLOW_COPY_AND_ASSIGN(AppsListRequest);
703 //============================= AppsDeleteRequest ==============================
705 // This class performs the request for deleting a Drive app.
706 // This request is mapped to
707 // https://developers.google.com/drive/v2/reference/files/trash
708 class AppsDeleteRequest : public EntryActionRequest {
709 public:
710 AppsDeleteRequest(RequestSender* sender,
711 const DriveApiUrlGenerator& url_generator,
712 const EntryActionCallback& callback);
713 ~AppsDeleteRequest() override;
715 // Required parameter.
716 const std::string& app_id() const { return app_id_; }
717 void set_app_id(const std::string& app_id) { app_id_ = app_id; }
719 protected:
720 // Overridden from UrlFetchRequestBase.
721 net::URLFetcher::RequestType GetRequestType() const override;
722 GURL GetURL() const override;
724 private:
725 const DriveApiUrlGenerator url_generator_;
726 std::string app_id_;
728 DISALLOW_COPY_AND_ASSIGN(AppsDeleteRequest);
731 //========================== ChildrenInsertRequest ============================
733 // This class performs the request for inserting a resource to a directory.
734 // This request is mapped to
735 // https://developers.google.com/drive/v2/reference/children/insert
736 class ChildrenInsertRequest : public EntryActionRequest {
737 public:
738 ChildrenInsertRequest(RequestSender* sender,
739 const DriveApiUrlGenerator& url_generator,
740 const EntryActionCallback& callback);
741 ~ChildrenInsertRequest() override;
743 // Required parameter.
744 const std::string& folder_id() const { return folder_id_; }
745 void set_folder_id(const std::string& folder_id) {
746 folder_id_ = folder_id;
749 // Required body.
750 const std::string& id() const { return id_; }
751 void set_id(const std::string& id) { id_ = id; }
753 protected:
754 // UrlFetchRequestBase overrides.
755 net::URLFetcher::RequestType GetRequestType() const override;
756 GURL GetURL() const override;
757 bool GetContentData(std::string* upload_content_type,
758 std::string* upload_content) override;
760 private:
761 const DriveApiUrlGenerator url_generator_;
762 std::string folder_id_;
763 std::string id_;
765 DISALLOW_COPY_AND_ASSIGN(ChildrenInsertRequest);
768 //========================== ChildrenDeleteRequest ============================
770 // This class performs the request for removing a resource from a directory.
771 // This request is mapped to
772 // https://developers.google.com/drive/v2/reference/children/delete
773 class ChildrenDeleteRequest : public EntryActionRequest {
774 public:
775 // |callback| must not be null.
776 ChildrenDeleteRequest(RequestSender* sender,
777 const DriveApiUrlGenerator& url_generator,
778 const EntryActionCallback& callback);
779 ~ChildrenDeleteRequest() override;
781 // Required parameter.
782 const std::string& child_id() const { return child_id_; }
783 void set_child_id(const std::string& child_id) {
784 child_id_ = child_id;
787 const std::string& folder_id() const { return folder_id_; }
788 void set_folder_id(const std::string& folder_id) {
789 folder_id_ = folder_id;
792 protected:
793 // UrlFetchRequestBase overrides.
794 net::URLFetcher::RequestType GetRequestType() const override;
795 GURL GetURL() const override;
797 private:
798 const DriveApiUrlGenerator url_generator_;
799 std::string child_id_;
800 std::string folder_id_;
802 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleteRequest);
805 //======================= InitiateUploadNewFileRequest =======================
807 // This class performs the request for initiating the upload of a new file.
808 class InitiateUploadNewFileRequest : public InitiateUploadRequestBase {
809 public:
810 // |parent_resource_id| should be the resource id of the parent directory.
811 // |title| should be set.
812 // See also the comments of InitiateUploadRequestBase for more details
813 // about the other parameters.
814 InitiateUploadNewFileRequest(RequestSender* sender,
815 const DriveApiUrlGenerator& url_generator,
816 const std::string& content_type,
817 int64 content_length,
818 const std::string& parent_resource_id,
819 const std::string& title,
820 const InitiateUploadCallback& callback);
821 ~InitiateUploadNewFileRequest() override;
823 // Optional parameters.
824 const base::Time& modified_date() const { return modified_date_; }
825 void set_modified_date(const base::Time& modified_date) {
826 modified_date_ = modified_date;
828 const base::Time& last_viewed_by_me_date() const {
829 return last_viewed_by_me_date_;
831 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
832 last_viewed_by_me_date_ = last_viewed_by_me_date;
834 const Properties& properties() const { return properties_; }
835 void set_properties(const Properties& properties) {
836 properties_ = properties;
839 protected:
840 // UrlFetchRequestBase overrides.
841 GURL GetURL() const override;
842 net::URLFetcher::RequestType GetRequestType() const override;
843 bool GetContentData(std::string* upload_content_type,
844 std::string* upload_content) override;
846 private:
847 const DriveApiUrlGenerator url_generator_;
848 const std::string parent_resource_id_;
849 const std::string title_;
851 base::Time modified_date_;
852 base::Time last_viewed_by_me_date_;
853 Properties properties_;
855 DISALLOW_COPY_AND_ASSIGN(InitiateUploadNewFileRequest);
858 //==================== InitiateUploadExistingFileRequest =====================
860 // This class performs the request for initiating the upload of an existing
861 // file.
862 class InitiateUploadExistingFileRequest : public InitiateUploadRequestBase {
863 public:
864 // |upload_url| should be the upload_url() of the file
865 // (resumable-create-media URL)
866 // |etag| should be set if it is available to detect the upload confliction.
867 // See also the comments of InitiateUploadRequestBase for more details
868 // about the other parameters.
869 InitiateUploadExistingFileRequest(RequestSender* sender,
870 const DriveApiUrlGenerator& url_generator,
871 const std::string& content_type,
872 int64 content_length,
873 const std::string& resource_id,
874 const std::string& etag,
875 const InitiateUploadCallback& callback);
876 ~InitiateUploadExistingFileRequest() override;
878 // Optional parameters.
879 const std::string& parent_resource_id() const { return parent_resource_id_; }
880 void set_parent_resource_id(const std::string& parent_resource_id) {
881 parent_resource_id_ = parent_resource_id;
883 const std::string& title() const { return title_; }
884 void set_title(const std::string& title) { title_ = title; }
885 const base::Time& modified_date() const { return modified_date_; }
886 void set_modified_date(const base::Time& modified_date) {
887 modified_date_ = modified_date;
889 const base::Time& last_viewed_by_me_date() const {
890 return last_viewed_by_me_date_;
892 void set_last_viewed_by_me_date(const base::Time& last_viewed_by_me_date) {
893 last_viewed_by_me_date_ = last_viewed_by_me_date;
895 const Properties& properties() const { return properties_; }
896 void set_properties(const Properties& properties) {
897 properties_ = properties;
900 protected:
901 // UrlFetchRequestBase overrides.
902 GURL GetURL() const override;
903 net::URLFetcher::RequestType GetRequestType() const override;
904 std::vector<std::string> GetExtraRequestHeaders() const override;
905 bool GetContentData(std::string* upload_content_type,
906 std::string* upload_content) override;
908 private:
909 const DriveApiUrlGenerator url_generator_;
910 const std::string resource_id_;
911 const std::string etag_;
913 std::string parent_resource_id_;
914 std::string title_;
915 base::Time modified_date_;
916 base::Time last_viewed_by_me_date_;
917 Properties properties_;
919 DISALLOW_COPY_AND_ASSIGN(InitiateUploadExistingFileRequest);
922 // Callback used for ResumeUpload() and GetUploadStatus().
923 typedef base::Callback<void(
924 const UploadRangeResponse& response,
925 scoped_ptr<FileResource> new_resource)> UploadRangeCallback;
927 //============================ ResumeUploadRequest ===========================
929 // Performs the request for resuming the upload of a file.
930 class ResumeUploadRequest : public ResumeUploadRequestBase {
931 public:
932 // See also ResumeUploadRequestBase's comment for parameters meaning.
933 // |callback| must not be null. |progress_callback| may be null.
934 ResumeUploadRequest(RequestSender* sender,
935 const GURL& upload_location,
936 int64 start_position,
937 int64 end_position,
938 int64 content_length,
939 const std::string& content_type,
940 const base::FilePath& local_file_path,
941 const UploadRangeCallback& callback,
942 const ProgressCallback& progress_callback);
943 ~ResumeUploadRequest() override;
945 protected:
946 // UploadRangeRequestBase overrides.
947 void OnRangeRequestComplete(const UploadRangeResponse& response,
948 scoped_ptr<base::Value> value) override;
949 // content::UrlFetcherDelegate overrides.
950 void OnURLFetchUploadProgress(const net::URLFetcher* source,
951 int64 current,
952 int64 total) override;
954 private:
955 const UploadRangeCallback callback_;
956 const ProgressCallback progress_callback_;
958 DISALLOW_COPY_AND_ASSIGN(ResumeUploadRequest);
961 //========================== GetUploadStatusRequest ==========================
963 // Performs the request to fetch the current upload status of a file.
964 class GetUploadStatusRequest : public GetUploadStatusRequestBase {
965 public:
966 // See also GetUploadStatusRequestBase's comment for parameters meaning.
967 // |callback| must not be null.
968 GetUploadStatusRequest(RequestSender* sender,
969 const GURL& upload_url,
970 int64 content_length,
971 const UploadRangeCallback& callback);
972 ~GetUploadStatusRequest() override;
974 protected:
975 // UploadRangeRequestBase overrides.
976 void OnRangeRequestComplete(const UploadRangeResponse& response,
977 scoped_ptr<base::Value> value) override;
979 private:
980 const UploadRangeCallback callback_;
982 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequest);
985 //======================= MultipartUploadNewFileDelegate =======================
987 // This class performs the request for initiating the upload of a new file.
988 class MultipartUploadNewFileDelegate : public MultipartUploadRequestBase {
989 public:
990 // |parent_resource_id| should be the resource id of the parent directory.
991 // |title| should be set.
992 // See also the comments of MultipartUploadRequestBase for more details
993 // about the other parameters.
994 MultipartUploadNewFileDelegate(base::SequencedTaskRunner* task_runner,
995 const std::string& title,
996 const std::string& parent_resource_id,
997 const std::string& content_type,
998 int64 content_length,
999 const base::Time& modified_date,
1000 const base::Time& last_viewed_by_me_date,
1001 const base::FilePath& local_file_path,
1002 const Properties& properties,
1003 const DriveApiUrlGenerator& url_generator,
1004 const FileResourceCallback& callback,
1005 const ProgressCallback& progress_callback);
1006 ~MultipartUploadNewFileDelegate() override;
1008 protected:
1009 // UrlFetchRequestBase overrides.
1010 GURL GetURL() const override;
1011 net::URLFetcher::RequestType GetRequestType() const override;
1013 private:
1014 const bool has_modified_date_;
1015 const DriveApiUrlGenerator url_generator_;
1017 DISALLOW_COPY_AND_ASSIGN(MultipartUploadNewFileDelegate);
1020 //====================== MultipartUploadExistingFileDelegate ===================
1022 // This class performs the request for initiating the upload of a new file.
1023 class MultipartUploadExistingFileDelegate : public MultipartUploadRequestBase {
1024 public:
1025 // |parent_resource_id| should be the resource id of the parent directory.
1026 // |title| should be set.
1027 // See also the comments of MultipartUploadRequestBase for more details
1028 // about the other parameters.
1029 MultipartUploadExistingFileDelegate(
1030 base::SequencedTaskRunner* task_runner,
1031 const std::string& title,
1032 const std::string& resource_id,
1033 const std::string& parent_resource_id,
1034 const std::string& content_type,
1035 int64 content_length,
1036 const base::Time& modified_date,
1037 const base::Time& last_viewed_by_me_date,
1038 const base::FilePath& local_file_path,
1039 const std::string& etag,
1040 const Properties& properties,
1041 const DriveApiUrlGenerator& url_generator,
1042 const FileResourceCallback& callback,
1043 const ProgressCallback& progress_callback);
1044 ~MultipartUploadExistingFileDelegate() override;
1046 protected:
1047 // UrlFetchRequestBase overrides.
1048 std::vector<std::string> GetExtraRequestHeaders() const override;
1049 GURL GetURL() const override;
1050 net::URLFetcher::RequestType GetRequestType() const override;
1052 private:
1053 const std::string resource_id_;
1054 const std::string etag_;
1055 const bool has_modified_date_;
1056 const DriveApiUrlGenerator url_generator_;
1058 DISALLOW_COPY_AND_ASSIGN(MultipartUploadExistingFileDelegate);
1061 //========================== DownloadFileRequest ==========================
1063 // This class performs the request for downloading of a specified file.
1064 class DownloadFileRequest : public DownloadFileRequestBase {
1065 public:
1066 // See also DownloadFileRequestBase's comment for parameters meaning.
1067 DownloadFileRequest(RequestSender* sender,
1068 const DriveApiUrlGenerator& url_generator,
1069 const std::string& resource_id,
1070 const base::FilePath& output_file_path,
1071 const DownloadActionCallback& download_action_callback,
1072 const GetContentCallback& get_content_callback,
1073 const ProgressCallback& progress_callback);
1074 ~DownloadFileRequest() override;
1076 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest);
1079 //========================== PermissionsInsertRequest ==========================
1081 // Enumeration type for specifying type of permissions.
1082 enum PermissionType {
1083 PERMISSION_TYPE_ANYONE,
1084 PERMISSION_TYPE_DOMAIN,
1085 PERMISSION_TYPE_GROUP,
1086 PERMISSION_TYPE_USER,
1089 // Enumeration type for specifying the role of permissions.
1090 enum PermissionRole {
1091 PERMISSION_ROLE_OWNER,
1092 PERMISSION_ROLE_READER,
1093 PERMISSION_ROLE_WRITER,
1094 PERMISSION_ROLE_COMMENTER,
1097 // This class performs the request for adding permission on a specified file.
1098 class PermissionsInsertRequest : public EntryActionRequest {
1099 public:
1100 // See https://developers.google.com/drive/v2/reference/permissions/insert.
1101 PermissionsInsertRequest(RequestSender* sender,
1102 const DriveApiUrlGenerator& url_generator,
1103 const EntryActionCallback& callback);
1104 ~PermissionsInsertRequest() override;
1106 void set_id(const std::string& id) { id_ = id; }
1107 void set_type(PermissionType type) { type_ = type; }
1108 void set_role(PermissionRole role) { role_ = role; }
1109 void set_value(const std::string& value) { value_ = value; }
1111 // UrlFetchRequestBase overrides.
1112 GURL GetURL() const override;
1113 net::URLFetcher::RequestType GetRequestType() const override;
1114 bool GetContentData(std::string* upload_content_type,
1115 std::string* upload_content) override;
1117 private:
1118 const DriveApiUrlGenerator url_generator_;
1119 std::string id_;
1120 PermissionType type_;
1121 PermissionRole role_;
1122 std::string value_;
1124 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest);
1127 //======================= SingleBatchableDelegateRequest =======================
1129 // Request that is operated by single BatchableDelegate.
1130 class SingleBatchableDelegateRequest : public UrlFetchRequestBase {
1131 public:
1132 // The instance takes ownership of |delegate|.
1133 SingleBatchableDelegateRequest(RequestSender* sender,
1134 BatchableDelegate* delegate);
1135 ~SingleBatchableDelegateRequest() override;
1137 private:
1138 GURL GetURL() const override;
1139 net::URLFetcher::RequestType GetRequestType() const override;
1140 std::vector<std::string> GetExtraRequestHeaders() const override;
1141 void Prepare(const PrepareCallback& callback) override;
1142 bool GetContentData(std::string* upload_content_type,
1143 std::string* upload_content) override;
1144 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override;
1145 void ProcessURLFetchResults(const net::URLFetcher* source) override;
1146 void OnURLFetchUploadProgress(const net::URLFetcher* source,
1147 int64 current,
1148 int64 total) override;
1149 scoped_ptr<BatchableDelegate> delegate_;
1151 // Note: This should remain the last member so it'll be destroyed and
1152 // invalidate its weak pointers before any other members are destroyed.
1153 base::WeakPtrFactory<SingleBatchableDelegateRequest> weak_ptr_factory_;
1155 DISALLOW_COPY_AND_ASSIGN(SingleBatchableDelegateRequest);
1158 //========================== BatchUploadRequest ==========================
1160 class BatchUploadChildEntry {
1161 public:
1162 explicit BatchUploadChildEntry(BatchableDelegate* request);
1163 ~BatchUploadChildEntry();
1164 scoped_ptr<BatchableDelegate> request;
1165 bool prepared;
1166 int64 data_offset;
1167 int64 data_size;
1169 private:
1170 DISALLOW_COPY_AND_ASSIGN(BatchUploadChildEntry);
1173 class BatchUploadRequest : public UrlFetchRequestBase {
1174 public:
1175 BatchUploadRequest(RequestSender* sender,
1176 const DriveApiUrlGenerator& url_generator);
1177 ~BatchUploadRequest() override;
1179 // Adds request to the batch request. The instance takes ownership of
1180 // |request|.
1181 void AddRequest(BatchableDelegate* request);
1183 // Completes building batch upload request, and starts to send the request to
1184 // server. Must add at least one request before calling |Commit|.
1185 void Commit();
1187 // Obtains weak pointer of this.
1188 base::WeakPtr<BatchUploadRequest> GetWeakPtrAsBatchUploadRequest();
1190 // Set boundary. Only tests can use this method.
1191 void SetBoundaryForTesting(const std::string& boundary);
1193 // Obtains reference to RequestSender that owns the request.
1194 RequestSender* sender() const { return sender_; }
1196 // Obtains URLGenerator.
1197 const DriveApiUrlGenerator& url_generator() const { return url_generator_; }
1199 // UrlFetchRequestBase overrides.
1200 void Prepare(const PrepareCallback& callback) override;
1201 void Cancel() override;
1202 GURL GetURL() const override;
1203 net::URLFetcher::RequestType GetRequestType() const override;
1204 std::vector<std::string> GetExtraRequestHeaders() const override;
1205 bool GetContentData(std::string* upload_content_type,
1206 std::string* upload_content) override;
1207 void ProcessURLFetchResults(const net::URLFetcher* source) override;
1208 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override;
1210 // content::UrlFetcherDelegate overrides.
1211 void OnURLFetchUploadProgress(const net::URLFetcher* source,
1212 int64 current,
1213 int64 total) override;
1215 private:
1216 typedef void* RequestID;
1217 // Obtains corresponding child entry of |request_id|. Returns NULL if the
1218 // entry is not found.
1219 ScopedVector<BatchUploadChildEntry>::iterator GetChildEntry(
1220 RequestID request_id);
1222 // Called after child requests' |Prepare| method.
1223 void OnChildRequestPrepared(RequestID request_id, DriveApiErrorCode result);
1225 // Complete |Prepare| if possible.
1226 void MayCompletePrepare();
1228 // Process result for each child.
1229 void ProcessURLFetchResultsForChild(RequestID id, const std::string& body);
1231 RequestSender* const sender_;
1232 const DriveApiUrlGenerator url_generator_;
1233 ScopedVector<BatchUploadChildEntry> child_requests_;
1235 PrepareCallback prepare_callback_;
1236 bool committed_;
1238 // Boundary of multipart body.
1239 std::string boundary_;
1241 // Multipart of child requests.
1242 ContentTypeAndData upload_content_;
1244 // Last reported progress value.
1245 int64 last_progress_value_;
1247 // Note: This should remain the last member so it'll be destroyed and
1248 // invalidate its weak pointers before any other members are destroyed.
1249 base::WeakPtrFactory<BatchUploadRequest> weak_ptr_factory_;
1251 DISALLOW_COPY_AND_ASSIGN(BatchUploadRequest);
1254 } // namespace drive
1255 } // namespace google_apis
1257 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_